Passport JWT authenticaten with cookie
up vote
0
down vote
favorite
I have set a cookie with
res.cookie("auth", token);
how can I get it in a passport.js JWT Strategy to autehnticate my user
(I know there are lots of tutorials around the internet, but I didn't find any fitting for me)
This is what I have at the moment:
passport.use(new JwtStrategy({
jwtFromRequest : req => req.cookies.auth,
secretOrKey : config.secret
},
function(jwt_payload, done){
if(jwt_payload.expires < Date.now()){
User.findById(jwt_payload.id, (err, user) =>{
if(err) return done(null, false);
if(user){
return done(null, true);
}
});
}
return done(null, false);
}));
[edit]
I have the code above in a separate file and I already performed a Test with Python and the cookie is stored in the browser
express authentication jwt passport.js
add a comment |
up vote
0
down vote
favorite
I have set a cookie with
res.cookie("auth", token);
how can I get it in a passport.js JWT Strategy to autehnticate my user
(I know there are lots of tutorials around the internet, but I didn't find any fitting for me)
This is what I have at the moment:
passport.use(new JwtStrategy({
jwtFromRequest : req => req.cookies.auth,
secretOrKey : config.secret
},
function(jwt_payload, done){
if(jwt_payload.expires < Date.now()){
User.findById(jwt_payload.id, (err, user) =>{
if(err) return done(null, false);
if(user){
return done(null, true);
}
});
}
return done(null, false);
}));
[edit]
I have the code above in a separate file and I already performed a Test with Python and the cookie is stored in the browser
express authentication jwt passport.js
Your extractor function is correct if simple (doesn't handle errors). If it's not working can you post the rest of your app config code so we can see if you're configuring cookies correctly?
– Paul
Nov 17 at 14:45
@Paul how would you solve this task? And in my case it alwys answers with a status code 401 Unauthorized if I'm trying to acess a protected route like: var auth = require('../auth/auth'); router.get('/test', auth.authenticate('jwt', { session: false }), write_controller.test);
– STAT1C
Nov 17 at 14:49
@timbiegeleisen thanks, I commented once, not sure why it posted twice. I deleted one. STATIC like I said, I need to see your middleware setup to tell you what's wrong. Please post all your relevant code.
– Paul
Nov 17 at 14:59
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have set a cookie with
res.cookie("auth", token);
how can I get it in a passport.js JWT Strategy to autehnticate my user
(I know there are lots of tutorials around the internet, but I didn't find any fitting for me)
This is what I have at the moment:
passport.use(new JwtStrategy({
jwtFromRequest : req => req.cookies.auth,
secretOrKey : config.secret
},
function(jwt_payload, done){
if(jwt_payload.expires < Date.now()){
User.findById(jwt_payload.id, (err, user) =>{
if(err) return done(null, false);
if(user){
return done(null, true);
}
});
}
return done(null, false);
}));
[edit]
I have the code above in a separate file and I already performed a Test with Python and the cookie is stored in the browser
express authentication jwt passport.js
I have set a cookie with
res.cookie("auth", token);
how can I get it in a passport.js JWT Strategy to autehnticate my user
(I know there are lots of tutorials around the internet, but I didn't find any fitting for me)
This is what I have at the moment:
passport.use(new JwtStrategy({
jwtFromRequest : req => req.cookies.auth,
secretOrKey : config.secret
},
function(jwt_payload, done){
if(jwt_payload.expires < Date.now()){
User.findById(jwt_payload.id, (err, user) =>{
if(err) return done(null, false);
if(user){
return done(null, true);
}
});
}
return done(null, false);
}));
[edit]
I have the code above in a separate file and I already performed a Test with Python and the cookie is stored in the browser
express authentication jwt passport.js
express authentication jwt passport.js
edited Nov 17 at 14:48
asked Nov 17 at 14:39
STAT1C
83
83
Your extractor function is correct if simple (doesn't handle errors). If it's not working can you post the rest of your app config code so we can see if you're configuring cookies correctly?
– Paul
Nov 17 at 14:45
@Paul how would you solve this task? And in my case it alwys answers with a status code 401 Unauthorized if I'm trying to acess a protected route like: var auth = require('../auth/auth'); router.get('/test', auth.authenticate('jwt', { session: false }), write_controller.test);
– STAT1C
Nov 17 at 14:49
@timbiegeleisen thanks, I commented once, not sure why it posted twice. I deleted one. STATIC like I said, I need to see your middleware setup to tell you what's wrong. Please post all your relevant code.
– Paul
Nov 17 at 14:59
add a comment |
Your extractor function is correct if simple (doesn't handle errors). If it's not working can you post the rest of your app config code so we can see if you're configuring cookies correctly?
– Paul
Nov 17 at 14:45
@Paul how would you solve this task? And in my case it alwys answers with a status code 401 Unauthorized if I'm trying to acess a protected route like: var auth = require('../auth/auth'); router.get('/test', auth.authenticate('jwt', { session: false }), write_controller.test);
– STAT1C
Nov 17 at 14:49
@timbiegeleisen thanks, I commented once, not sure why it posted twice. I deleted one. STATIC like I said, I need to see your middleware setup to tell you what's wrong. Please post all your relevant code.
– Paul
Nov 17 at 14:59
Your extractor function is correct if simple (doesn't handle errors). If it's not working can you post the rest of your app config code so we can see if you're configuring cookies correctly?
– Paul
Nov 17 at 14:45
Your extractor function is correct if simple (doesn't handle errors). If it's not working can you post the rest of your app config code so we can see if you're configuring cookies correctly?
– Paul
Nov 17 at 14:45
@Paul how would you solve this task? And in my case it alwys answers with a status code 401 Unauthorized if I'm trying to acess a protected route like: var auth = require('../auth/auth'); router.get('/test', auth.authenticate('jwt', { session: false }), write_controller.test);
– STAT1C
Nov 17 at 14:49
@Paul how would you solve this task? And in my case it alwys answers with a status code 401 Unauthorized if I'm trying to acess a protected route like: var auth = require('../auth/auth'); router.get('/test', auth.authenticate('jwt', { session: false }), write_controller.test);
– STAT1C
Nov 17 at 14:49
@timbiegeleisen thanks, I commented once, not sure why it posted twice. I deleted one. STATIC like I said, I need to see your middleware setup to tell you what's wrong. Please post all your relevant code.
– Paul
Nov 17 at 14:59
@timbiegeleisen thanks, I commented once, not sure why it posted twice. I deleted one. STATIC like I said, I need to see your middleware setup to tell you what's wrong. Please post all your relevant code.
– Paul
Nov 17 at 14:59
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53352243%2fpassport-jwt-authenticaten-with-cookie%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Your extractor function is correct if simple (doesn't handle errors). If it's not working can you post the rest of your app config code so we can see if you're configuring cookies correctly?
– Paul
Nov 17 at 14:45
@Paul how would you solve this task? And in my case it alwys answers with a status code 401 Unauthorized if I'm trying to acess a protected route like: var auth = require('../auth/auth'); router.get('/test', auth.authenticate('jwt', { session: false }), write_controller.test);
– STAT1C
Nov 17 at 14:49
@timbiegeleisen thanks, I commented once, not sure why it posted twice. I deleted one. STATIC like I said, I need to see your middleware setup to tell you what's wrong. Please post all your relevant code.
– Paul
Nov 17 at 14:59