Code Monkey home page Code Monkey logo

nodejs-jwt-token-authentication-api's Introduction

nodejs-jwt-token-authentication-api

This repository provides jwt token authentication and provides various APIs like User Registration,Login etc.

Installation

Run this command to install the required dependency.

npm install 

Create a .env file in root directory and add following constant

  1. Create an environment variable MONGODB_URL and add mongodb connection url
  2. Create an environment variable API_KEY and add random string
  3. Create an environment variable JWT_KEY and add random string,this variable will be used to sign the JWT Token

Run this command to start the server

npm start

Api

User Registration
User Login
Fetch User using Jwt Token
Delete User Using Jwt Token

To run the above API using PostMan you can download using this link and Import in Postman

User Registration API.


This Api creates a user in the MongoDB

Api Endpoint

http://api_domain:3000/api/v1/auth/signup

Headers

In order to use this endpoint, you need to pass apiKey that we have created previously under .env file in the header

apikey: "api_key"

Sample Request Body

{
"FirstName":"sam",
"LastName":"stan",
"Email":"[email protected]",
"Password":"test123",
"Address":"Canada",
"Phone":"918701887142",
"Role":"Admin"
}

Output

{
    "IsPosted": true,
    "Success": true
}

User Login API


This Api return JWT Token After successful Login

Api Endpoint

http://api_domain:3000/api/v1/auth/login

Headers

In order to use this endpoint, you need to pass apiKey that we have created previously under .env file in the header

apikey: "api_key"

Sample Request Body

{
"Email":"[email protected]",
"Password":"test123"
}

Output

{
    "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJFbWFpbCI6InRlc3RAbWFpbDcuaW8iLCJVc2VySWQiOiI2MjkxY2JmYTQ2OTgyNzMwOTFkOTZiZjQiLCJpYXQiOjE2NTM3MjQzMTcsImV4cCI6MTY1MzcyNzkxN30.pTYhNNB3zDonLw7X5tWLIAKlQ8jFGSSh5iNyqfi4pTA",
    "IsAuthenticated": true
}

Fetch User using Jwt Token


This Api will fetch the user profile using JWT Token

Api Endpoint

http://api_domain:3000/api/v1/auth/getUserByToken

Headers

In order to use this endpoint, you need to pass apiKey and JWT Token, that we have created previously under .env file in the header

apikey: "api_key"
Authorization: Bearer jwt_token

Output

{
    "Profile": [
        {
            "IsDeleted": false,
            "Address": "Canada",
            "Phone": 918701887145,
            "FirstName": "sam",
            "LastName": "stan",
            "Role": "Admin",
            "_id": "6291cbfa4698273091d96bf4",
            "Email": "[email protected]",
            "CreatedAt": "2022-05-28T07:15:06.391Z"
        }
    ],
    "Success": true
}

Delete User Using Jwt Token


This Api deletes the user using JWT Token and user id

Api Endpoint

http://api_domain:3000/api/v1/auth/delete

Headers

In order to use this endpoint, you need to pass apiKey that we have created previously under .env file in the header

apikey: "api_key"

Query Param

user_id=6291cbfa4698273091d96bf4 Note: the user id will be _id in profile response

Output

{
    "IsDeleted": true,
    "Success": true
}

nodejs-jwt-token-authentication-api's People

Contributors

dependabot[bot] avatar indrasen715 avatar productfarm avatar

Stargazers

 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.