From 10609c02f051e30bc72f9c761e7109cc47b7907c Mon Sep 17 00:00:00 2001 From: vicwere Date: Wed, 29 May 2024 10:41:52 +0300 Subject: [PATCH] Refresh & Revoke Tokens --- models/n_users.js | 3 +- passport-config.js | 5 +- routes/processes/nishauri_new.js | 91 +++++++++++++++++++++++++++++++- 3 files changed, 96 insertions(+), 3 deletions(-) diff --git a/models/n_users.js b/models/n_users.js index d068780..d5df7ba 100644 --- a/models/n_users.js +++ b/models/n_users.js @@ -25,7 +25,8 @@ const NUsers = sequelize.sequelize.define( otp_gen_hour: Sequelize.TIME, profile_otp_number:Sequelize.STRING, profile_otp_date:Sequelize.DATEONLY, - profile_status: Sequelize.STRING + profile_status: Sequelize.STRING, + refresh_token: Sequelize.STRING }, { timestamps: true, diff --git a/passport-config.js b/passport-config.js index b789b27..1180a41 100644 --- a/passport-config.js +++ b/passport-config.js @@ -20,7 +20,10 @@ module.exports = passport => { // console.log(jwt_payload.username); const user = NUsers.findOne({ where: { - id: jwt_payload.username + id: jwt_payload.username, + refresh_token: { + [Sequelize.Op.ne]: null // Sequelize operator for 'not equal to null' + } } }); diff --git a/routes/processes/nishauri_new.js b/routes/processes/nishauri_new.js index 815c437..f7f4253 100644 --- a/routes/processes/nishauri_new.js +++ b/routes/processes/nishauri_new.js @@ -20,6 +20,8 @@ require("dotenv").config(); //const Op = require("sequelize"); const { Op } = require("sequelize"); var bcrypt = require("bcrypt"); +const crypto = require('crypto'); + //const Sequelize = require("sequelize"); @@ -168,6 +170,91 @@ router.post("/signup", async (req, res) => { } }); + +//Token Refresh +router.post("/refreshtoken", async (req, res) => { + let refreshToken = req.body.token; + let _user_id = req.body.user_id; + + try { + console.log(_user_id); + + let user = NUsers.findOne({ + where: { + id: base64.decode(_user_id), + refresh_token:refreshToken + } + }); + + if (!user) { + return res.status(403).json({ message: "Invalid refresh token" }); + }else + { + let newToken = jwt.sign( + { username: _user_id }, + process.env.JWT_SECRET, + { expiresIn: "3h" } + ); + + let newRefreshToken = crypto.randomBytes(64).toString('hex'); + + var l = { + user_id: base64.encode(_user_id), + token: newToken, + refreshToken: newRefreshToken, + }; + + let today = moment(new Date().toDateString()).format("YYYY-MM-DD HH:mm:ss"); + const log_login = await NUsers.update( + { last_login: today, refresh_token:newRefreshToken }, + { where: { id: base64.decode(_user_id) } } + ); + + return res.status(200).json({ + success: true, + msg: "New access token generated", + data: l + }); + + + + } + } catch (err) { + return res.status(400).json({ msg: "Error Occurred While Generating Token" }); + + } + + }); + + //Token Revocation + router.post("/revoke_token",async (req, res) => { + let refreshToken = req.body.token; + let _user_id = req.body.user_id; + + let user = NUsers.findOne({ + where: { + id: base64.decode(_user_id), + refresh_token:refreshToken + } + }); + //onst user = users.find((u) => u.refreshToken === refreshToken); + if (!user) { + return res.status(400).json({ message: "Invalid refresh token" }); + } + const log_login = await NUsers.update( + { refresh_token:null }, + { where: { id: base64.decode(_user_id) } } + ); + var l = { + user_id: base64.encode(_user_id) + }; + return res.status(200).json({ + success: true, + msg: "Logout Successful", + data: l + }); + }); + //Sign-In Users router.post("/signin", async (req, res) => { let vusername = req.body.user_name; @@ -228,8 +315,9 @@ router.post("/signin", async (req, res) => { }; try { + const refreshToken = crypto.randomBytes(64).toString('hex'); const log_login = await NUsers.update( - { last_login: today }, + { last_login: today, refresh_token:refreshToken }, { where: { id: check_username.id } } ); const token = jwt.sign( @@ -246,6 +334,7 @@ router.post("/signin", async (req, res) => { user_id: base64.encode(check_username.id), page_id: 1, token: token, + refreshToken: refreshToken, account_verified: check_username.is_active };