Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- router.post('/authenticate', (req, res, next) => {
- const username = req.body.username;
- const password = req.body.password;
- User.getUserByUsername(username, (err, user) => {
- if (err) throw err;
- if (!user) {
- return res.json({ success: false, msg: 'User not found' });
- }
- User.comparePassword(password, user.password, (err, isMatch) => {
- if (err) throw err;
- if (isMatch) {
- const token = jwt.sign({ data: user }, config.secret, {
- expiresIn: 604800
- })
- res.json({
- success: true,
- token: 'JWT ' + token,
- user: {
- id: user._id,
- name: user.name,
- username: user.username,
- email: user.email,
- status: user.status
- }
- });
- } else {
- return res.json({ success: false, msg: 'Wrong password' });
- }
- });
- });
- });
- router.get('/profile', passport.authenticate('jwt', { session: false }), (req, res, next) => {
- res.json({ user: req.user });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement