Thinkful-Ed / node-jwt-auth

15 stars 118 forks source link

Unable to access req.user server side #6

Open megmatty opened 6 years ago

megmatty commented 6 years ago

Tried several things, how do I get access to req.user on the server side once logged in? Trying to save data to database with a user id attached to it, but can't get the user id.

oampo commented 6 years ago

You should have access to req.user as long as the endpoints are protected by the JWT strategy. For example:

app.get('/some-resource',
    passport.authenticate('jwt'), // The middleware both protects you endpoint, and sets req.user
    (req, res) => {
        console.log(req.user);
        res.json({foo: 'bar'});
    }
);