2 years ago

#63573

test-img

DevChris

ExpressJS - PassportJS LocalStrategy: Get current logged in user

I have an expressjs server where I'm using the Passport Local strategy. But something isn't working probably

My Login Route in login.js File

router.post('/login', passport.authenticate('local', {
    successRedirect: '/v2/api/',
    failureRedirect: '/v2/api/loginfailed',
    failureFlash: true
}), async (req, res, next) => {
});

And I want to get the information about the current logged in user in another route (dashboard.js)

router.post('/dashboard', async (req, res) => {
    try {
        if (req.user) {
            res.json({ user: req.user })
        } else {
            res.status(403).send({
                message: 'Not authenticated failed'
            });
        }
    } catch {
        res.status(400).send({
            message: 'Request failed'
        });
        throw new Error('Error')
    }
});

On Execution: If I'm logging in via /login and calling the /dashboard Route, it response me only :

{
"user": {}
}

Did I missed out something or is it just not even possible what I want to do?

Thanks for your Answers :)

node.js

express

passport.js

passport-local

0 Answers

Your Answer

Accepted video resources