Advertisement
Guest User

Untitled

a guest
Mar 7th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. var requireLogin = function (req, res, next) {
  2. if (req.session.user) {
  3. next();
  4. } else {
  5. res.redirect('/');
  6. }
  7. }
  8.  
  9. app.post('/login', requireLogin, routes.login);
  10.  
  11. exports.login = function (req, res) {
  12. var query = {username:req.body.username, password:req.body.password};
  13.  
  14. User.find(query, function (err, data) {
  15. if (data) {
  16. req.session.user = data;
  17. console.log(data);
  18. res.redirect('/home');
  19. } else {
  20. console.log(err);
  21. res.redirect('/');
  22. }
  23. });
  24. };
  25.  
  26. app.post('/login', requireLogin, routes.login);
  27.  
  28. exports.login = function (req, res) {
  29. var query = {username:req.body.username, password:req.body.password};
  30. User.findOne(query, function (err, data) {
  31. if (data && !err) {
  32. req.session.user = data;
  33. console.log(data);
  34. res.redirect('/home');
  35. } else {
  36. console.log(err);
  37. res.redirect('/');
  38. }
  39. });
  40. };
  41.  
  42. User.findOne(query, function (err, data) {
  43. if (err) {
  44. // the username/password could be valid (or not),
  45. // but there's no way to tell because there was some server error
  46. console.log(err);
  47. res.redirect('/');
  48. } else if (data) {
  49. // a user was found that matched the query
  50. req.session.user = data;
  51. console.log(data);
  52. res.redirect('/home');
  53. } else {
  54. // there is no User that matches the query
  55. res.redirect('/');
  56. }
  57. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement