Skip to content

anoopcodehack/auth-system

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 

Repository files navigation

πŸŽ“ Authentication & Authorization System β€” Learning Platform

A secure backend built with Node.js, Express, MongoDB, JWT, and bcrypt.


πŸ“ Project Structure

auth-system/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ config/
β”‚   β”‚   └── db.js              # MongoDB connection
β”‚   β”œβ”€β”€ controllers/
β”‚   β”‚   β”œβ”€β”€ authController.js  # Register & Login logic
β”‚   β”‚   └── userController.js  # User CRUD operations
β”‚   β”œβ”€β”€ middleware/
β”‚   β”‚   └── auth.js            # JWT verify + Role guard
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   └── User.js            # Mongoose schema (auto-hashes password)
β”‚   β”œβ”€β”€ routes/
β”‚   β”‚   β”œβ”€β”€ authRoutes.js      # /api/auth/*
β”‚   β”‚   └── userRoutes.js      # /api/users/*
β”‚   └── server.js              # Entry point
β”œβ”€β”€ .env
β”œβ”€β”€ package.json
└── README.md

βš™οΈ Setup Instructions

1. Prerequisites

  • Node.js v18+
  • MongoDB (local or MongoDB Atlas)

2. Install Dependencies

npm install

3. Configure Environment

Edit .env with your values:

PORT=5000
MONGO_URI=mongodb://localhost:27017/learning_platform
JWT_SECRET=your_super_secret_key_here
JWT_EXPIRES_IN=7d

4. Run the Server

# Development (auto-restart)
npm run dev

# Production
npm start

Server starts at: http://localhost:5000


πŸ” How It Works

Concept Implementation
Password Hashing bcryptjs with salt rounds = 12
Token JWT signed with JWT_SECRET, expires in 7 days
Token Location Authorization: Bearer <token> header
Role Guard restrictTo('admin') middleware on protected routes

πŸ“‘ API Endpoints

Auth Routes (Public)

Method Endpoint Description
POST /api/auth/register Register a new user
POST /api/auth/login Login and get JWT token

User Routes (Protected β€” requires Bearer token)

Method Endpoint Role Description
GET /api/users/me Student / Admin Get own profile
PUT /api/users/me Student / Admin Update own profile
GET /api/users Admin only Get all users
GET /api/users/:id Admin only Get any user by ID
DELETE /api/users/:id Admin only Delete any user

πŸ“¨ Sample Requests & Responses

1. Register a Student

Request:

POST /api/auth/register
Content-Type: application/json

{
  "name": "Rahul Sharma",
  "email": "[email protected]",
  "password": "secret123",
  "role": "student"
}

Response (201):

{
  "message": "Registration successful.",
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "user": {
    "id": "6639f3a1b8e6a20012345678",
    "name": "Rahul Sharma",
    "email": "[email protected]",
    "role": "student"
  }
}

2. Login

Request:

POST /api/auth/login
Content-Type: application/json

{
  "email": "[email protected]",
  "password": "secret123"
}

Response (200):

{
  "message": "Login successful.",
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "user": {
    "id": "6639f3a1b8e6a20012345678",
    "name": "Rahul Sharma",
    "email": "[email protected]",
    "role": "student"
  }
}

3. Get My Profile (Student or Admin)

Request:

GET /api/users/me
Authorization: Bearer <your_token>

Response (200):

{
  "user": {
    "_id": "6639f3a1b8e6a20012345678",
    "name": "Rahul Sharma",
    "email": "[email protected]",
    "role": "student",
    "bio": "",
    "createdAt": "2024-05-01T10:00:00.000Z"
  }
}

4. Update My Profile

Request:

PUT /api/users/me
Authorization: Bearer <your_token>
Content-Type: application/json

{
  "name": "Rahul S.",
  "bio": "I love learning!"
}

Response (200):

{
  "message": "Profile updated successfully.",
  "user": {
    "_id": "6639f3a1b8e6a20012345678",
    "name": "Rahul S.",
    "bio": "I love learning!",
    "role": "student"
  }
}

5. Admin: Get All Users

Request:

GET /api/users
Authorization: Bearer <admin_token>

Response (200):

{
  "count": 2,
  "users": [
    { "_id": "...", "name": "Rahul Sharma", "email": "[email protected]", "role": "student" },
    { "_id": "...", "name": "Admin User", "email": "[email protected]", "role": "admin" }
  ]
}

6. Admin: Delete a User

Request:

DELETE /api/users/6639f3a1b8e6a20012345678
Authorization: Bearer <admin_token>

Response (200):

{
  "message": "User \"Rahul Sharma\" deleted successfully."
}

7. Access Denied (403 Example)

A student trying to access admin routes:

Response (403):

{
  "message": "Access denied. Only [admin] can perform this action."
}

8. No Token (401 Example)

Response (401):

{
  "message": "Access denied. No token provided."
}

πŸ§ͺ Testing with Postman / Thunder Client

  1. Register a user β†’ copy the token
  2. In subsequent requests, add header: Authorization: Bearer <token>
  3. Register an admin with "role": "admin" to test admin routes

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors