Creating the user registration endpoint for our CCA app – Part 6
authFunctions.js
authFunctions.js
The provided code is a Node.js module that exports a memberSignup func on for registering new members in a
system. Here’s a summary of its main components:
Impor ng Required Modules: The bcrypt module for password hashing, dotenv for environment variable
management, and the Member model from the local ‘Database/member’ file are imported.
memberSignup Func on: This is an asynchronous func on that takes in a request object (req), a role, and a
response object (res). It performs the following steps:
Validates if the member name is not already taken by calling the validateMemberName func on.
Validates if the email is not already registered by calling the validateEmail func on.
If either valida on fails, it sends a 400 status response with an appropriate error message.
If valida ons pass, it hashes the password using bcrypt.hash, creates a new Member instance with the
request data, hashed password, and role, and saves it to the database.
If the member is successfully saved, it sends a 201 status response with a success message.
If any error occurs during the process, it sends a 500 status response with the error message.
validateMemberName Func on: This is an asynchronous func on that checks if a member name is already taken. It
queries the database for a member with the given name and returns false if found, true otherwise.
validateEmail Func on: This is an asynchronous func on that checks if an email is already registered. It queries the
database for a member with the given email and returns false if found, true otherwise.
Module Export: The memberSignup func on is exported for use in other parts of the applica on.
userRouter.js
userRouter.js
The provided code is a Node.js module that sets up and exports an Express router with two routes for member
registra on. Here’s a summary of its main components:
Impor ng Required Modules: The Express Router func on and the memberSignup func on from the local
‘Controller/authFunc ons’ file are imported.
Se ng Up Routes: Two POST routes are set up on the router:
/register-member: This route calls the memberSignup func on with the request body, the role “member”, and the
response object. This is used to register a new member.
/register-president: This route calls the memberSignup func on with the request body, the role “president”, and
the response object. This is used to register a new president.
Expor ng the Router: The configured router is exported for use in other parts of the applica on.
(Remember to uncomment the lines related to userRouter.js in server.js)