Skip to content

Commit

Permalink
Refresh & Revoke Tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
vicwere committed May 29, 2024
1 parent 61c8660 commit 10609c0
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 3 deletions.
3 changes: 2 additions & 1 deletion models/n_users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion passport-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
});

Expand Down
91 changes: 90 additions & 1 deletion routes/processes/nishauri_new.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -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
};

Expand Down

0 comments on commit 10609c0

Please sign in to comment.