This Project demonstrate the Authenication module with Redis implmentation.
The purpose of this project is to implemented the redis with the NestJs
It is build with NestJs framework with MongoDB as Database and Redis database to store and verify OTP authenication.
Tech should be installed before running the application
- NestJs
- Docker
- Redis
- MongoDB
- Postman (To execute the APIs)
After cloning the project
Install Npm libraries using npm i
Add .env
file in the root folder with variables
DB_URL
#REDIS
REDIS_HOST
REDIS_PORT
Run redis in docker
Connect with Database
Run npm run start:dev
to run the application
If everything is good you won't see red lines else you have stackoverflow to debug.
This is build with NestJs framework. I have created auth module which includes CRUD APIs with login and OTP verfication which is implmented using redis database. Later I have discussed more about redis.
Here, User can do the following actions:
- Create User
- Login User
- Get all Users
- Get User by Id
- Update User
- Delete User
- Request Otp for User
- Verfiy Otp for User
I have use argon2 library for hashing. Redis for OTP verfication as it gives more feature above normal database.
It stores at key value pair so you don't have to manage the occurence of redundany. It has Time to live (TTL) which can be used to expire otp in certain specified time.
Copy and paste below code in a file with .json
extension and import it in the postman
{
"info": {
"_postman_id": "68a8f2e8-24a7-4533-9337-1df99e9f17ad",
"name": "RedisOtp",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "24806169"
},
"item": [
{
"name": "Get users",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{base_url}}/users",
"host": [
"{{base_url}}"
],
"path": [
"users"
]
}
},
"response": []
},
{
"name": "Create user",
"request": {
"method": "GET",
"header": []
},
"response": []
},
{
"name": "Login user",
"request": {
"method": "GET",
"header": []
},
"response": []
},
{
"name": "Get user by id",
"request": {
"method": "GET",
"header": []
},
"response": []
},
{
"name": "UpdateUser",
"request": {
"method": "PATCH",
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"userName\":\"newUser\"\r\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{base_url}}/users/update?userId=63f6014e256f9ecc6586f296",
"host": [
"{{base_url}}"
],
"path": [
"users",
"update"
],
"query": [
{
"key": "userId",
"value": "63f6014e256f9ecc6586f296"
}
]
}
},
"response": []
},
{
"name": "Delete User",
"request": {
"method": "GET",
"header": []
},
"response": []
},
{
"name": "Generate OTP",
"request": {
"method": "GET",
"header": []
},
"response": []
},
{
"name": "Verify OTP",
"request": {
"method": "GET",
"header": []
},
"response": []
}
]
}
- I have added dtos for some APIs you can added them
- Tokens were not scope of my agenda but you can added them for session
- Caching can be added for quick response like for getAll users
Please add suggestion if you thing something is not right. Help us to improve