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










share|improve this question
























  • 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















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










share|improve this question
























  • 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













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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Ottavio Pratesi

Tricia Helfer

15 giugno