What am I doing wrong
See original GitHub issueI don’t know what happened, but my Passport Jwt Auth is not working.
in app.js
app.use(passport.initialize());
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (user, done) {
done(null, user);
});
passport.use(new JwtStrategy({
secretOrKey: jwtSecret,
jwtFromRequest: JwtExtract.fromAuthHeaderAsBearerToken()
}, function (payload, done) {
console.log("Auth : JwtStrategy");
moduleUser.User.findById(payload._doc._id)
.exec()
.then(function (user) {
if (user) done(null, user);
else done(null, false);
}
)
.catch(function (err) {
return done(err, false);
});
}
));
NOTE: console.log("Auth : JwtStrategy");
never gets called
My Login API
router.post('/login', function (req, res) {
if (req.body.username && req.body.password) {
moduleUser.User.findOne({username: req.body.username, password: req.body.password})
.exec()
.then(function (user) {
if (user) {
res.json({
success: true,
message: 'Login successful',
token: 'JWT ' + jwt.sign(user.toObject(), jwtSecret, {expiresIn: '14 days'})
});
} else {
res.json({
success: false,
message: 'Please enter valid login details'
});
}
})
} else {
res.status(401).json({
error: 'All information not provided'
});
}
});
And my Not working Private route
router.get('/private', passport.authenticate('jwt', {session: false}),
function (req, res) {
res.send('respond with a resource!!!!');
}
);
`
I Always receive 401 as a response
URL: http://localhost:5000/users/private
Headers-> Authorization: JWT eyJhb…
Am I doing anything wrong here? Any help is appreciated
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (1 by maintainers)
Top Results From Across the Web
I feel stuck in life, what am I doing wrong? - Quora
The only thing you are doing wrong is if you are not putting any effort for coming out. We all get stuck and...
Read more >Ep #142: Finding the Answer: What Am I Doing Wrong?
I'm sharing how to talk to any shame you're experiencing, and how to define success, failure, and everything in between.
Read more >What Am I Doing Wrong? - Troubleshoot Your Dating Life for ...
Ever wonder, “What Am I Doing Wrong?” when it comes to dating and love. Find out how you can improve your dating and...
Read more >What am I doing wrong? : r/socialskills - Reddit
What am I doing wrong ? Hello everybody. First, I just wanted to say 'hi' to ...
Read more >"What am I doing wrong in my relationship": 13 most common ...
“What am I doing wrong in my relationship”: 13 most common mistakes. We sometimes include products we think are useful for our readers....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@ThunderRoid the readme says:
The scheme of Bearer is like I commented above. Please try it and let me know 😉
Ah, looks like that bit of the README fell out of date. I will correct this.