Advertisement
Guest User

Untitled

a guest
Jul 16th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. / Authenticate
  2. router.post('/authenticate', (req, res, next) => {
  3. const email = req.body.email;
  4. const password = req.body.password;
  5.  
  6. User.getUserByEmail(email, (err, user) => {
  7. if(err) throw err;
  8. if(!user){
  9. return res.json({success: false, msg: 'User not found'});
  10. }
  11. /*check if user account is active*/
  12. if(!user.active)
  13. return res.json({success: false, msg: 'Please Verify You Account!'});
  14. User.comparePassword(password, user.password, (err, isMatch) => {
  15. if(err) throw err;
  16. if(isMatch){
  17. const token = jwt.sign({data: user}, config.secret, {
  18. expiresIn: 604800 // 1 week
  19. });
  20.  
  21. res.json({
  22. success: true,
  23. token: `Bearer ${token}`,
  24. user: {
  25. id: user._id,
  26. name: user.name,
  27. username: user.username,
  28. email: user.email
  29. }
  30. });
  31. } else {
  32. return res.json({success: false, msg: 'Wrong password'});
  33. }
  34. });
  35. });
  36. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement