This repository provides jwt token authentication and provides various APIs like User Registration,Login etc.
Run this command to install the required dependency.
npm install
Create a .env
file in root directory and add following constant
- Create an environment variable
MONGODB_URL
and add mongodb connection url - Create an environment variable
API_KEY
and add random string - 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
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
This Api creates a user in the MongoDB
http://api_domain:3000/api/v1/auth/signup
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"
{
"FirstName":"sam",
"LastName":"stan",
"Email":"[email protected]",
"Password":"test123",
"Address":"Canada",
"Phone":"918701887142",
"Role":"Admin"
}
{
"IsPosted": true,
"Success": true
}
This Api return JWT Token After successful Login
http://api_domain:3000/api/v1/auth/login
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"
{
"Email":"[email protected]",
"Password":"test123"
}
{
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJFbWFpbCI6InRlc3RAbWFpbDcuaW8iLCJVc2VySWQiOiI2MjkxY2JmYTQ2OTgyNzMwOTFkOTZiZjQiLCJpYXQiOjE2NTM3MjQzMTcsImV4cCI6MTY1MzcyNzkxN30.pTYhNNB3zDonLw7X5tWLIAKlQ8jFGSSh5iNyqfi4pTA",
"IsAuthenticated": true
}
This Api will fetch the user profile using JWT Token
http://api_domain:3000/api/v1/auth/getUserByToken
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
{
"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
}
This Api deletes the user using JWT Token and user id
http://api_domain:3000/api/v1/auth/delete
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"
user_id=6291cbfa4698273091d96bf4
Note: the user id will be _id
in profile response
{
"IsDeleted": true,
"Success": true
}