Saturday, April 13, 2024
 Popular · Latest · Hot · Upcoming
108
rated 0 times [  112] [ 4]  / answers: 1 / hits: 7542  / 3 Years ago, sat, april 17, 2021, 12:00:00

I want the jwt token to expire after 1 minute.
But when I run my code it doesn't work.


how can i fix my code?


it's little confused.. i want to expired 1minute later....
I want the jwt token to expire after 1 minute.
But when I run my code it doesn't work.


how can i fix my code?


it's little confused.. i want to expired 1minute later....


(auth.js)


    const express = require("express");
const passport = require("passport");
const bcrypt = require("bcrypt");
// const User = require("../models/user");
const jwt = require("jsonwebtoken");
const { User, Post, Comment, Image } = require("../models");
const { isLoggedIn, isNotLoggedIn } = require("./middlewares");
const { Op } = require("sequelize");

const router = express.Router();

router.get("/kakao", passport.authenticate("kakao"));

router.get(
"/kakao/callback",
passport.authenticate("kakao", {
session: false,
}),
(req, res, next) => {
const { snsId, provider } = req.user;

const token = jwt.sign({ id: snsId, provider }, "jwt-secret-key", {
expiresIn: 60 * 60,
});
res.redirect(`demo://app?accessToken=${token}`);

}
);

(passport/index.js)


    const passport = require("passport");
const { Strategy: LocalStrategy } = require("passport-local");
const { ExtractJwt, Strategy: JWTStrategy } = require("passport-jwt");
const bcrypt = require("bcrypt");
const User = require("../models/user");
const kakao = require("./kakaoStrategy");

const JWTConfig = {
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
// jwtFromRequest: ExtractJwt.fromHeader("Authorization"),
secretOrKey: "jwt-secret-key",
};

const JWTVerify = async (jwtPayload, done) => {
try {

const user = await User.findOne({ where: { snsId: jwtPayload.id } });

if (user) {
done(null, user);
return;
}

done(null, false, { reason: "올바르지 않은 인증정보 입니다." });
} catch (error) {
console.error(error);
done(error);
}
};

kakao();

module.exports = () => {
passport.use("jwt", new JWTStrategy(JWTConfig, JWTVerify));
};

More From » node.js

 Answers
6
var jwt = require('jsonwebtoken');
var token = jwt.sign({ foo: 'bar' }, 'secret key',{expiresIn: 1}); // 1 sec
console.log(token);
setTimeout(()=>{
try {
var decoded = jwt.verify(token, 'secret key');
console.log(decoded)
} catch(err) {
console.log('error',err)
}
},2000);

refer https://www.npmjs.com/package/jsonwebtoken for more details


[#1467] Saturday, April 10, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
marcos

Total Points: 331
Total Questions: 106
Total Answers: 104

Location: Gabon
Member since Sat, Jul 25, 2020
4 Years ago
marcos questions
Thu, Feb 24, 22, 00:00, 2 Years ago
Mon, Feb 1, 21, 00:00, 3 Years ago
Tue, Jan 26, 21, 00:00, 3 Years ago
Wed, Jan 6, 21, 00:00, 3 Years ago
Tue, May 26, 20, 00:00, 4 Years ago
;