This repository contains an authentication API built using Express.js and MongoDB, enabling user registration, login, and access control using JSON Web Tokens (JWT).
-
Authentication APIs:
-
Signup: Register a new user with name, email, password, and role (e.g., Student).
- Endpoint:
POST http://localhost:4000/api/v1/signup
- Example Request Body:
{ "name": "Abhi", "email": "Abhi@gmail.com", "password": "123456", "role": "Student" }
- Endpoint:
-
Login: Authenticate a user with email and password.
- Endpoint:
POST http://localhost:4000/api/v1/login
- Example Request Body:
{ "email": "abhishek-admin@gmail.com", "password": "abhishekPassword" }
- Endpoint:
-
-
Protected Routes:
- Utilizes middleware (
authMiddleware
) to protect routes based on user roles.- Example Protected Routes:
GET http://localhost:4000/api/v1/test
GET http://localhost:4000/api/v1/student/
GET http://localhost:4000/api/v1/admin/
- Example Protected Routes:
- Utilizes middleware (
-
JWT Token Usage:
- JWT tokens are used for authentication and authorization across routes.
- Example tokens are provided for testing different user roles (
student
,admin
).
- Example tokens are provided for testing different user roles (
- JWT tokens are used for authentication and authorization across routes.
-
Watch all SCREENSHOTS for Testing result of Api's
-
Signup:
POST http://localhost:4000/api/v1/signup
-
Login:
POST http://localhost:4000/api/v1/login
-
Test Route (Protected):
GET http://localhost:4000/api/v1/test
-
Student Route (Protected):
GET http://localhost:4000/api/v1/student/
-
Admin Route (Protected):
GET http://localhost:4000/api/v1/admin/
The API handles JWT tokens from various sources (cookies, body, or headers) to authenticate users. Example token extraction:
// Extract JWT Token from either cookie, body, or header
const token = req.cookies.token || req.body.token || req.header("Authorization").replace("Bearer ", "");
-
Clone Repository:
git clone https://github.com/abhixsliet/backend-task.git cd backend-task
-
Install Dependencies:
npm install
-
Set Environment Variables:
- Create a
.env
file in the root directory. - Define the following environment variables:
PORT=4000 MONGODB_URI=your_mongodb_connection_string JWT_SECRET=your_jwt_secret
- Note: Ensure to replace
your_mongodb_connection_string
with your MongoDB cluster URI. If you do not have one, follow these steps:- Sign up or log in to MongoDB Atlas (https://www.mongodb.com/cloud/atlas).
- Create a new cluster (or use an existing one).
- Obtain the connection string by navigating to "Connect" and selecting "Connect your application".
- Create a
-
Start the Server:
npm run dev
express
: Fast, unopinionated, minimalist web framework for Node.jsmongoose
: MongoDB object modeling tool designed to work in an asynchronous environmentjsonwebtoken
: JSON Web Token implementation for Node.jsbcryptjs
: Library to hash passwords
Please adjust the setup and configuration based on your specific environment and requirements. If you have any questions or need further assistance, feel free to reach out!
This project is licensed under the MIT License.