Reputation: 51
Im using JWT token in my node js application for authentication and when a new token is generated Iam udpating in DB with fields custid,token,sessionValid(boolean).
Now I've set the token expiry to 5 minutes.So once the token is expired it should automatically update the db record and make sessionValid to false.Here I have a rest call to update the DB.The reason for going this approach is Im implementing SSO and the other application would use this same rest call for session validation.
Below is the code how Im generating token
var isExpired = false;
router.post('/auth', passport.authenticate(
'local', {
session: false
})generateToken, inserttoken,respond);
function generateToken(req, res, next) {
token = jwt.sign({
id: req.user.id,
},SECRET, {
expiresIn: 5 * 60
});
req.token = token;
next();
}
function inserttoken(req,res,next){
//In this function Im doing rest call to update the db with the token generated
}
function respond(req, res) {
res.cookie('customerid', req.user.id, { expires: new Date(new Date().getTime()+1*60*
1000),httpOnly: true })
res.cookie('token', req.token, { expires: new Date(new Date().getTime()+1*60*
1000), httpOnly: true })
res.status(200).json({
user: req.user,
token: req.token
});
}
So how can I automatically automatically do my rest call and update db when token is expired. Iam even saving these in my cookies as well.
Any help appreciated.Thanks!
Upvotes: 0
Views: 601
Reputation: 119
If you are using angular for frontend in controller you can can call method again and again in interval and send that in backend to check if its valid or not,No Need to store JWT token.or other option is to store JWT in cookie and set expiration time on expiry call rest..
Upvotes: 0
Reputation: 952
If you're storing the token then you are not following sessionless. So it's not kinda RESTful.
JWT token is based on signature which can be verified, Need to store. To maintain a session, keep updating token on front-end withing expiry time.
Please follow this link for better understanding
Hope that helps!
Upvotes: 1