Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Complete Node.js authentication boilerplate with email verification, password management, JWT token-based authentication, and Google OAuth support. Also containerized using docker

Notifications You must be signed in to change notification settings

rahulstech/node-authentication-with-email-verification

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 

Repository files navigation

NodeJS Authentication with Email Verification and OAuth

This project provides a robust and secure authentication system for your Node.js applications, featuring email verification, password management, and OAuth integration (Google). It emphasizes security best practices, including token-based authentication with asymmetric key signing and password hashing.

Table of Contents

Features

  • User Registration:
    • Email and Password registration with mandatory email verification.
    • Google OAuth registration.
  • Email Verification:
    • Time-limited verification links (15 minutes).
    • Resend verification email functionality.
    • Change email address with verification.
  • Password Management:
    • Secure password hashing using bcrypt.
    • Password reset functionality with time-limited reset links (5 minutes).
    • Change password functionality.
  • Authentication:
    • Token-based authentication (JWT).
    • Short-lived access tokens (15 minutes).
    • Long-lived refresh tokens (7 days) for seamless token renewal.
    • Access tokens are sent via the Authorization header (Bearer token).
  • Security:
    • Protection against common vulnerabilities.
  • Scalability:
    • Designed for scalability using Redis for caching.

Go to Table of Contents

Tech Stack

  • Backend:
    • Node.js
    • Express.js
    • Passport.js (for authentication strategies)
  • Database:
    • MySQL (with Sequelize ORM)
  • Caching:
    • Redis
  • Email:
    • AWS SES
  • Authentication & Authorization:
    • JWT (JSON Web Tokens)
    • Google OAuth 2.0
  • Validation:
    • Joi
  • Other:
    • Bcrypt (for password hashing)

Go to Table of Contents

Installation

  1. Clone the Repository:

    git clone https://github.com/rahulstech/node-authentication-with-email-verification.git
    
    cd node-authentication-with-email-verification
  2. Environment Variables:

    • Copy example.env-dev to .env-dev and example.env-prod to .env-prod
    • Fill in the required credentials:
      • Google OAuth Client ID and Secret

      • AWS SES credentials (IAM user with SES permissions)

      • Redis host and port (defaults are usually fine)

        # Example .env file
        GOOGLE_CLIENT_ID=your_google_client_id
        GOOGLE_CLIENT_SECRET=your_google_client_secret
        AMAZON_ID=your_aws_iam_id
        AMAZON_SECRET=your_aws_iam_secret
        AMAZON_REGION=your_aws_region
        EMAIL_VERIFICATION_SENDER=your_verified_ses_email
        REDIS_HOST=localhost
        REDIS_PORT=6379
        
  3. JWT Keys:

    • Generate RSA key pair for JWT signing (using OpenSSL):

      openssl genpkey -algorithm RSA -out jwt_private.pem -pgenopt rsa:key_gen_bits:4096
      openssl rsa -in jwt_private.pem -pubout -out jwt_public.pem
    • Place jwt_private.pem and jwt_public.pem in the secrets directory. (Create the secrets directory if it doesn't exist.)

    Development

    1. Install Dependencies:

      npm install
    2. Database Setup:

      • Configure MySQL connection in config/config.json.

      • Create the database and run migrations:

        npx sequelize-cli db:create
        npx sequelize-cli db:migrate
    3. Run the Dev Server:

      npm run dev

      The server will typically start on port 5000 (configurable in .env-dev).

    Go to Table of Contents

    Production

    1. Run Docker:

      • Run the following command from the project root directory

        docker-compose up -d # -d will run containers in detached mode. remove -d if you don't want to run in detached mode

    Go to Table of Contents

API Endpoints

POST /register - Register a new user
POST /login - Login a user with email and password
GET /login/google - Login via google
GEt /google/callback - Web hook used by google oauth server on authenticated
GET /dashboard - 
POST /refresh - Generates new access token based on sent refresh token in request body
GET /verify/email/link - Send a new email verification link to registered email, requires login
GET /verify/email - Verify email
PATCH /email/new - Change email, requires login
POST /password/reset/link - Generate the password reset link
PATCH /password/reset - Reset password if forget
PATCH /password/new - Change password, requires log in
GET /logout - Log out, requires login

Go to Table of Contents

Releases

No releases published

Packages

No packages published

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.