Skip to content

Auth Controller

This Node.js module provides authentication functionalities for user sign-up and login using bcrypt for password hashing, jsonwebtoken for generating JWTs, and jsonschema for validating request bodies.

signup Function:

  • Validates incoming user data against userRegisterSchema.
  • Checks if the email already exists in the database.
  • Hashes the password using bcrypt.
  • Saves the new user to the database.
  • Generates a JWT token containing user details.
  • Returns a success response with the user’s data and token.

login Function:

  • Validates login data against userAuthSchema.
  • Authenticates the user by comparing the provided password with the hashed password stored in the database.
  • If authentication succeeds, generates a JWT token containing user details.
  • Returns the login success response with user data and token.

Key Points:

  • Utilizes bcrypt for secure password hashing.
  • Uses jsonwebtoken for creating secure, encoded tokens representing user session data.
  • Implements jsonschema to ensure the integrity and format of incoming data.
  • Provides secure and efficient methods for user authentication and session management.
  • Implements error handling to provide meaningful feedback for incorrect or invalid inputs.