This is a Node.js-based authentication API that allows users to sign up, log in, and access protected routes based on their roles (Admin, Student, Visitor). The application is deployed on Render.
This project demonstrates user authentication and role-based access control using Node.js, Express, and MongoDB. It includes features for user registration, login, and protected routes that can only be accessed by authenticated users with specific roles.
-
Clone the repository:
git clone https://github.com/2115425Amar/AuthApp.git cd node-auth-api
-
Install the dependencies:
npm install
-
Set up the environment variables. Create a
.env
file in the root directory and add the following:PORT=3000 MONGO_URI=your_mongodb_uri JWT_SECRET=your_jwt_secret
Ensure you have MongoDB installed and running on your local machine, or use a cloud-based MongoDB service like MongoDB Atlas.
-
Start the server:
npm start
-
The server will start on the port specified in the
.env
file (default is 3000). -
Navigate to
http://localhost:3000
to see the home page.
-
Home
GET /
: Returns a welcome message.
-
Authentication
POST /api/v1/signup
: User sign-up.POST /api/v1/login
: User login.
-
Test Route
GET /api/v1/test
: Accessible to authenticated users.
-
Student Route
GET /api/v1/student
: Accessible only to users with the "Student" role.
-
Admin Route
GET /api/v1/admin
: Accessible only to users with the "Admin" role.
-
Authentication Middleware: Checks for the presence of a valid JWT token in cookies, request body, or headers. If the token is missing or invalid, returns a 401 Unauthorized response.
-
Role-based Middleware:
- isStudent: Ensures the user has the "Student" role.
- isAdmin: Ensures the user has the "Admin" role.
The User model is defined in models/User.js
with the following schema:
- name: String (required)
- email: String (required)
- password: String (required)
- role: String (enum: ["Admin", "Student", "Visitor"])
This project is licensed under the MIT License. See the LICENSE file for details.
Deployed Application: Authentication App