question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

What am I doing wrong

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
nexigcommented, Nov 27, 2017

@ThunderRoid the readme says:

bearer

The scheme of Bearer is like I commented above. Please try it and let me know 😉

0reactions
mikenicholsoncommented, Mar 13, 2018

Ah, looks like that bit of the README fell out of date. I will correct this.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found