Skip to content

An authentication example app using Node.js, Express, and MongoDB, featuring user management, login, and role-based access control.

License

Notifications You must be signed in to change notification settings

mathisdev7/auth-example

Repository files navigation

Auth Example Project

This project is an authentication example built with Node.js, Express, and MongoDB. It demonstrates user signup, login, and role-based access control using JWT for authentication.

Features

  • User Signup
  • User Login
  • Role-based Access Control
  • JWT Authentication
  • User Deletion (Admin only)

Getting Started

Prerequisites

  • Node.js
  • MongoDB

Installation

  1. Clone the repository:

    git clone https://github.com/mathisdev7/auth-example
  2. Navigate to the project directory:

    cd auth-example
  3. Install the dependencies:

    npm install

    or

    pnpm install

    or

    yarn add
    
  4. Create a .env file in the root directory and add your MongoDB URI and JWT secret:

    MONGO_URI=your_mongo_uri
    JWT_SECRET=your_jwt_secret
    

Running the Project

To start the server, run:

npm start

or

pnpm start

or

yarn start

The server will be running on http://localhost:3000.

Running Tests

To run the tests, use:

npm test

or

pnpm test

or

yarn test

API Documentation

Signup

  • Endpoint: /api/auth/signup

  • Method: POST

  • Request Body:

    {
      "name": "string",
      "email": "string",
      "password": "string",
      "username": "string",
      "role": "user" | "admin"
    }
  • Response:

    • 201 Created on success
    • 400 Bad Request if user already exists or validation fails

Login

  • Endpoint: /api/auth/login
  • Method: POST
  • Request Body:
    {
      "email": "string",
      "password": "string"
    }
  • Response:
    • 200 OK with JWT token on success
    • 400 Bad Request if credentials are incorrect

Get Current User

  • Endpoint: /api/users/me
  • Method: GET
  • Headers:
    • Authorization: Bearer <token>
  • Response:
    • 200 OK with user data
    • 401 Unauthorized if token is missing or invalid

Delete User

  • Endpoint: /api/users/delete/:id
  • Method: GET
  • Headers:
    • Authorization: Bearer <token>
  • Response:
    • 200 OK if user is deleted
    • 403 Forbidden if the user is not an admin
    • 404 Not Found if user does not exist

Types

User

interface IUser {
  name: string;
  email: string;
  password: string;
  username: string;
  role: "user" | "admin";
  createdAt: Date;
  updatedAt: Date;
}

Custom Request

interface CustomRequest extends express.Request {
  user?: {
    _id: string;
    name: string;
    email: string;
    password: string;
    username: string;
    role: string;
  };
}

License

This project is licensed under the MIT License.

About

An authentication example app using Node.js, Express, and MongoDB, featuring user management, login, and role-based access control.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published