Giter VIP home page Giter VIP logo

abhixsliet / backend-task Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 507 KB

This repository features a backend project focusing on authentication using Express and MongoDB. It includes APIs for user authentication (login, signup, user details) and implements protected routes with JWT token-based authentication.

License: MIT License

JavaScript 100.00%
api express express-authentication express-authorizations express-middleware jsonwebtoken cookies

backend-task's Introduction

Authentication API using Express and MongoDB

This repository contains an authentication API built using Express.js and MongoDB, enabling user registration, login, and access control using JSON Web Tokens (JWT).

Features

  1. 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": "[email protected]",
            "password": "123456",
            "role": "Student"
        }
    • Login: Authenticate a user with email and password.

      • Endpoint: POST http://localhost:4000/api/v1/login
      • Example Request Body:
        {
            "email": "[email protected]",
            "password": "abhishekPassword"
        }
  2. 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/
  3. JWT Token Usage:

    • JWT tokens are used for authentication and authorization across routes.
      • Example tokens are provided for testing different user roles (student, admin).

Routes (Tested with Postman)

  • 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/

Token Handling

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 ", "");

Setup

  1. Clone Repository:

    git clone https://github.com/abhixsliet/backend-task.git
    cd backend-task
  2. Install Dependencies:

    npm install
  3. 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".
  4. Start the Server:

    npm run dev

Dependencies

  • express: Fast, unopinionated, minimalist web framework for Node.js
  • mongoose: MongoDB object modeling tool designed to work in an asynchronous environment
  • jsonwebtoken: JSON Web Token implementation for Node.js
  • bcryptjs: 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!

License

This project is licensed under the MIT License.

backend-task's People

Contributors

abhixsliet avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.