This project is a backend User Authentication System built using Node.js, Express.js, MongoDB, Mongoose, bcryptjs, and JSON Web Token (JWT).
It allows users to register, login, and access protected routes using JWT-based authentication.
This project was developed as part of my internship task at Synctecxhub.
- User signup
- User login
- Password hashing using bcryptjs
- JWT token generation after login
- Protected routes using authentication middleware
- Token expiry handling
- Invalid credentials handling
- Input validation
- Proper HTTP status codes and response messages
- API testing using Postman
- Node.js
- Express.js
- MongoDB
- Mongoose
- bcryptjs
- JSON Web Token
- dotenv
- Nodemon
- Postman
auth-system/ │ ├── models/ │ └── User.js │ ├── routes/ │ └── authRoutes.js │ ├── middleware/ │ └── authMiddleware.js │ ├── .env.example ├── server.js ├── package.json └── README.md
git clone https://github.com/your-username/your-repository-name.git
cd your-repository-name
npm install
Create a .env file in the root folder and add the following:
PORT=5000 MONGO_URI=mongodb://127.0.0.1:27017/auth_system_db JWT_SECRET=your_jwt_secret_key JWT_EXPIRES_IN=1h
npm run dev
The server will run on:
| Method | Endpoint | Description | Protected |
|---|---|---|---|
| POST | /api/auth/signup |
Register a new user | No |
| POST | /api/auth/login |
Login user and get JWT token | No |
| GET | /api/auth/profile |
Get logged-in user profile | Yes |
POST /api/auth/signup
{ "username": "john", "email": "john@example.com", "password": "123456" }
{ "success": true, "message": "User registered successfully", "data": { "id": "USER_ID", "username": "john", "email": "john@example.com" } }
201 Created
POST /api/auth/login
{ "email": "john@example.com", "password": "123456" }
{ "success": true, "message": "Login successful", "token": "YOUR_JWT_TOKEN", "data": { "id": "USER_ID", "username": "john", "email": "john@example.com" } }
200 OK
GET /api/auth/profile
In Postman, go to the Authorization tab:
Type: Bearer Token
Token: Paste your JWT token here
Or add this in Headers:
Authorization: Bearer YOUR_JWT_TOKEN
{ "success": true, "message": "Protected profile accessed successfully", "data": { "_id": "USER_ID", "username": "john", "email": "john@example.com", "createdAt": "2026-05-14T00:00:00.000Z", "updatedAt": "2026-05-14T00:00:00.000Z" } }
200 OK
{ "success": false, "message": "Username, email and password are required" }
Status Code:
400 Bad Request
{ "success": false, "message": "Email already registered" }
Status Code:
409 Conflict
{ "success": false, "message": "Invalid email or password" }
Status Code:
401 Unauthorized
{ "success": false, "message": "Access denied. No token provided." }
Status Code:
401 Unauthorized
{ "success": false, "message": "Invalid token. Authentication failed." }
Status Code:
401 Unauthorized
{ "success": false, "message": "Token expired. Please login again." }
Status Code:
401 Unauthorized
| Status Code | Meaning |
|---|---|
| 200 | Request successful |
| 201 | User registered successfully |
| 400 | Bad request or validation error |
| 401 | Unauthorized request |
| 409 | Email already exists |
| 500 | Server error |
- Open Postman.
- Test the signup API using
POST /api/auth/signup. - Test the login API using
POST /api/auth/login. - Copy the JWT token from the login response.
- Go to the protected profile API using
GET /api/auth/profile. - Add the token in the Authorization tab as a Bearer Token.
- Send the request and check the protected user data.
- Passwords are not stored in plain text.
- bcryptjs is used to hash passwords before storing them in MongoDB.
- JWT is generated after successful login.
- Protected routes can only be accessed with a valid token.
- Expired and invalid tokens are handled properly.
- Add role-based access control
- Add refresh token functionality
- Add forgot password and reset password feature
- Add email verification
- Add stronger password validation
- Add logout functionality
- Add user profile update feature
This project was completed as part of my internship at Synctecxhub.
The main goal of this task was to understand backend authentication, secure password handling, JWT-based authorization, protected routes, and API testing using Postman.
Vikas Kushwaha