Last active
June 6, 2018 20:45
-
-
Save danielkhan/e65f1e4b0de3674e406cd01f4cf07363 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
router.post('/login', async (req, res) => { | |
try { | |
return passport.authenticate('local', { session: false }, (err, user) => { | |
if (err || !user) { | |
res.status(404).json({ error: 'Invalid username or password' }); | |
} | |
return req.login(user, { session: false }, (loginErr) => { | |
if (loginErr) { | |
res.status(500).json({ error: loginErr }); | |
} | |
const token = jwt.sign({ userId: user._id }, config.JWT_SECRET); | |
return res.json({ data: { user, token } }); | |
}); | |
})(req, res); // <-- !!!! | |
} catch (err) { | |
logger.fatal(err); | |
return res.status(500).json({ error: err }); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment