This Auth
class is used to create the authentication flow. It extends the ErrorHandler
class to handle errors and has all the required functions to create a basic authentication. The class provides the following functionality:
This function allows the user to create the signup flow.
req
: The request object sent from the client.res
: The response object sent to the client.next
: The function to pass control to the next middleware function.Model
: The database schema to be used.Body
: The values to be inserted in the database.
This asynchronous function registers a user, sends an email verification link, and returns a token.
This function allows the user to initiate the login flow.
req
: The request object sent from the client.res
: The response object sent to the client.next
: The function to pass control to the next middleware function.Model
: The database schema to be used.
This asynchronous function logs in a user by checking their email and password, verifying their email status, and sending a token for authentication.
This function allows the user to logout and clears the current session of the user.
req
: The request object sent from the client.res
: The response object sent to the client.next
: The function to pass control to the next middleware function.
The function logs out a user by setting the token cookie to null and returning a success message.
This function allows the user to change the password by receiving a token in email.
req
: The request object sent from the client.res
: The response object sent to the client.next
: The function to pass control to the next middleware function.Model
: The database schema to be used.
If the user is not found in the database, an error message with status code 404 is returned. If the email is successfully sent to the user, a success message with status code 200 is returned. If there is an error sending the email, an error message with status code 500 is returned.
This function allows the user to reset the password using the token.
req
: The request object sent from the client.res
: The response object sent to the client.next
: The function to pass control to the next middleware function.Model
: The database schema to be used.
If the user is not found, an error message "password reset token is invalid or expired" is returned. If the password and confirmPassword do not match, an error message "password does not match" is returned. If there are no errors, a token is sent with a status code of 201.
This function verifies a user's token and updates their verification status in the database.
req
: The request object sent from the client.res
: The response object sent to the client.next
: The function to pass control to the next middleware function.Model
: The database schema to be used.
If the user is not found, an error message is returned with the message "verification token is invalid or expired". If the user is found and successfully verified, a token is sent with a status code of 201.