Skip to content

Ankittkr/WDC-Induction

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Backend Development Assignment

Authentication System with JWT & MySQL

This project implements a secure backend authentication system using Node.js, Express.js, MySQL, and JWT. It includes user authentication, profile management, and token-based authorization.

The system follows a RESTful API architecture and ensures security through bcrypt password hashing and JWT authentication.


Task Requirements Implemented

1. Authentication System

The backend provides secure APIs for user signup and login.

Features

  • Secure user registration
  • Secure login authentication
  • Password hashing using bcrypt
  • Input validation for required fields
  • Prevention of duplicate users

APIs

Signup

POST /users/register

Request Body

{
  "name": "dummy",
  "email": "dummy@gmail.com",
  "password": "dummy123"
}

Features

  • Validates required fields
  • Hashes password using bcrypt
  • Stores user in MySQL database

Login

POST /users/login

Request Body

{
  "email": "dummy@gmail.com",
  "password": "dummy123"
}

Features

  • Verifies password using bcrypt
  • Generates JWT access and refresh tokens
  • Stores refresh token in database
  • Returns authenticated user details

2. Database Integration & CRUD

The application integrates with a MySQL database and performs CRUD operations for user profiles.

Database

MySQL is used to store user information.

Users Table Schema

CREATE TABLE users (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(100),
  email VARCHAR(100) UNIQUE,
  password VARCHAR(255),
  bio TEXT,
  contact VARCHAR(20),
  refreshToken VARCHAR(255),
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

User Profile APIs

Get User Profile

GET /users/profile

Returns authenticated user information.

Response

{
  "id": 1,
  "name": "dummy",
  "email": "dummy@gmail.com",
  "bio": "Developer",
  "contact": "1234567890"
}

Update Profile

PATCH /users/update-profile

Allows updating user profile fields.

Example Request

{
  "name": "Dummy Updated",
  "bio": "Full Stack Developer"
}

Features

  • Updates only provided fields
  • Dynamic SQL query building
  • Returns updated profile

Delete User

DELETE /users/profile

Deletes the user account and clears authentication cookies.


3. JWT Authentication Flow

The system uses JSON Web Tokens (JWT) for secure authentication.

Two tokens are used:

Access Token

  • Short lived
  • Used for accessing protected routes

Refresh Token

  • Long lived
  • Stored in database
  • Used to generate new access tokens

Token Generation

During login:

  1. User credentials are verified.

  2. Server generates:

    • Access Token
    • Refresh Token
  3. Refresh token is saved in the database.

  4. Tokens are sent via HTTP-only cookies.


Refresh Token API

POST /users/refresh-token

This endpoint verifies the refresh token and generates a new access token.

Response

{
  "accessToken": "...",
  "refreshToken": "..."
}

Token Verification

A middleware verifies the JWT token for protected routes.

The middleware:

  1. Reads the access token from cookies.
  2. Verifies the token using JWT secret.
  3. Extracts user information.
  4. Attaches the user to req.user.

Authenticated User Endpoint

Protected routes can access authenticated user details.

Example

req.user.id
req.user.email

Used in endpoints like:

GET /users/profile

Technologies Used

Backend

  • Node.js
  • Express.js

Database

  • MySQL

Authentication

  • JWT (jsonwebtoken)
  • bcrypt

Utilities

  • Custom error handling
  • Async middleware wrapper

Project Structure

src
│
├── controllers
│   ├── auth.controller.js
│   └── users.controllers.js
│
├── db
│   └── index.js
│
├── middlewares
│   └── auth.middleware.js
│
├── routes
│   └── user.routes.js
│
├── utils
│   ├── ApiError.js
│   ├── ApiResponse.js
│   ├── asyncHandler.js
│   └── token.js
│
├── app.js
└── index.js

Security Measures

  • Password hashing using bcrypt
  • JWT based authentication
  • HTTP-only cookies
  • Refresh token stored in database
  • Protected routes with authentication middleware

How to Run the Project

Install dependencies

npm install

Configure environment variables

Create .env

PORT=5000

DB_HOST=localhost
DB_USER=root
DB_PASSWORD=password
DB_NAME=wdc_backend

ACCESS_TOKEN_SECRET=your_access_secret
ACCESS_TOKEN_EXPIRY=1d

REFRESH_TOKEN_SECRET=your_refresh_secret
REFRESH_TOKEN_EXPIRY=7d

Start the server

npm run dev

Server will run on:

http://localhost:5000

Author

Ankit Kumar

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors