Git Product home page Git Product logo

hamidbyte / user-registration-jwt-auth Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 59 KB

This API, built with Node.js, Express, and MongoDB, facilitates seamless user registration, account activation, and secure login functionalities. Utilizing JSON Web Tokens (JWT) for authentication and authorization, it ensures protected access to endpoints, offering a comprehensive solution for user management.

JavaScript 97.99% CSS 0.57% EJS 1.44%
account-activation authentication authorization backend backend-api express json-web-token jwt jwt-auth jwt-authentication

user-registration-jwt-auth's Introduction

User Registration JWT Auth

This API, built with Node.js, Express, and MongoDB, facilitates seamless user registration, account activation, and secure login functionalities. Utilizing JSON Web Tokens (JWT) for authentication and authorization, it ensures protected access to endpoints, offering a comprehensive solution for user management.

Prerequisites

Make sure you have the following software installed on your machine:

  • Node.js (version 14 or higher)
  • npm (comes with Node.js)
  • MongoDB (Install locally or use MongoDB Atlas for a cloud-based solution.)
  • Git (optional but recommended)

Getting Started

  1. Clone the repository:

    git clone https://github.com/HamidByte/User-Registration-JWT-Auth.git
  2. Navigate to the Project Directory:

    cd User-Registration-JWT-Auth
  3. Install Dependencies:

    npm install
  4. Configure Environment Variables:

    Create a .env file in the root of the project and set the variables:

  5. Update Configurations:

    Modify the configuration files in the config directory according to your needs.

  6. Update your scripts in package.json for a cross-platform solution:

    If you are working in a Windows environment and using the Command Prompt, you should use the set command to set environment variables.

     "scripts": {
       "start": "nodemon index.js",
       "dev": "set NODE_ENV=development && nodemon index.js",
       "prod": "set NODE_ENV=production && nodemon index.js",
     }

    If you are working in a Unix-like environment (Linux or macOS), you should use the export command to set environment variables.

     "scripts": {
       "start": "nodemon index.js",
       "dev": "export NODE_ENV=development && nodemon index.js",
       "prod": "export NODE_ENV=production && nodemon index.js",
     }

    Make sure to check your environment and use the appropriate command accordingly.

  7. Run the Application:

  • For development:

    npm start

    or

    npm run dev
  • For production:

    npm run prod

The server will run at http://localhost:3000 by default.

Endpoints

Register User

  • Endpoint: /register
  • Method: POST
  • Description: Register a new user with a first name, last name, email address, and password.
  • Request Body:
    {
      "firstName": "John",
      "lastName": "Doe",
      "email": "[email protected]",
      "password": "password123"
    }
  • Response:
    {
      "message": "User registered successfully. Please check your email for activation."
    }

Activate Account

  • Endpoint: /activate/:token
  • Method: GET
  • Description: Activate a user account using the activation token sent via email.
  • Parameters:
    • token: Activation token received via email.
  • Response:
    {
      "message": "Account activated successfully."
    }

Resend Activation Link

  • Endpoint: /resend-activation
  • Method: POST
  • Description: Resend the activation link to a user who hasn't activated the account within the specified time.
  • Request Body:
    {
      "email": "[email protected]"
    }
  • Response:
    {
      "message": "Activation link resent successfully. Please check your email for activation."
    }

User Login

  • Endpoint: /login
  • Method: POST
  • Description: Authenticate the user and generate a JWT token.
  • Request Body:
    {
      "email": "[email protected]",
      "password": "password123"
    }
  • Response:
    {
      "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...."
    }

Protected Profile Route

  • Endpoint: /profile
  • Method: GET
  • Description: Access the user's profile route. Requires a valid JWT token.
  • Authorization Header: Bearer Token
  • Response:
    {
      "message": "Profile route accessed by user with ID: <userId>"
    }

Forget Password

  • Endpoint: /forget-password
  • Method: POST
  • Description: Initiate the process of resetting the user's password by sending a reset link to their email.
  • Request Body:
    {
      "email": "[email protected]"
    }
  • Response:
    {
      "message": "Reset link sent successfully. Please check your email."
    }

Reset Password

  • Endpoint: /reset-password/:token
  • Method: POST
  • Description: Reset the user's password using the reset token sent via email.
  • Parameters:
    • token: Reset token received via email.
  • Request Body:
    {
      "newPassword": "newPassword123"
    }
  • Response:
    {
      "message": "Password reset successful."
    }

404 Page Not Found

  • Description: Catch-all route to handle 404 errors.
  • Response:
    {
      "message": "Page not found"
    }

This catch-all route will respond with a 404 status and a JSON message indicating that the requested page is not found.

Middleware

JWT Verification Middleware

  • Middleware Function: verifyToken
  • Description: Verifies the JWT token included in the Authorization header. Applied to protected routes.

Notes

  • Ensure to include the JWT token in the Authorization header as Bearer Token for protected routes.
  • Use the provided activation link or resend-activation endpoint for account activation.
  • For security reasons, keep sensitive information such as JWT secret keys and email credentials in a secure configuration file (e.g., environment variables).

user-registration-jwt-auth's People

Contributors

hamidbyte avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.