You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

34 lines
913 B

  1. const debug = require('debug')('express:login');
  2. const passport = require('passport');
  3. const {
  4. USERNAME_PASSWORD_COMBINATION_ERROR,
  5. INTERNAL_SERVER_ERROR,
  6. SUCCESSFULLY_LOGGED_IN,
  7. } = require('../constants');
  8. function login(req, res, next) {
  9. // debug('login');
  10. return passport.authenticate('local', (error, user) => {
  11. if (error || !user) {
  12. req.session.messages = {
  13. errors: { invalidEmailOrPassword: USERNAME_PASSWORD_COMBINATION_ERROR },
  14. };
  15. return res.status(401).redirect('/login');
  16. }
  17. return req.logIn(user, loginError => {
  18. if (loginError) {
  19. req.session.messages = {
  20. errors: { internalServerError: INTERNAL_SERVER_ERROR },
  21. };
  22. return res.status(500).redirect('/login');
  23. }
  24. req.session.messages = { loggedIn: SUCCESSFULLY_LOGGED_IN };
  25. return next();
  26. });
  27. })(req, res, next);
  28. }
  29. module.exports = login;