Skip to content

indrasen715/nodejs-jwt-token-authentication-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •