Advertisement
Guest User

Untitled

a guest
Sep 26th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. app.post("/login", function (req, res, next) {
  2. res.type = "application/json";
  3.  
  4. if (loginAttempts[req.ip] && loginAttempts[req.ip].length > loginAttemptLimit) {
  5. var hour = 3600000;
  6. if (Date.now() - loginAttempts[req.ip][0] > hour)
  7. loginAttempts[req.ip] = [];
  8. else
  9. {
  10. res.locals.json.success = false;
  11. res.locals.json.message = "You have failed to login too many times in the last hour. Please wait 60 minutes and try again.";
  12. next();
  13. }
  14. }
  15.  
  16. if (req.body.email && req.body.password) {
  17. //Check email and password
  18. var query = User.findOne({ "email": req.body.email }, "", function (err, user) {
  19. if (error.Handle(err)) {
  20. res.locals.json.success = false;
  21. res.locals.json.message = "There was an internal error while processing your login request. Please try again.";
  22. next()
  23. }
  24.  
  25. if (user && bcrypt.compareSync(req.body.password, user.password)) {
  26. loginAttempts[req.ip] = [];
  27.  
  28. //log session info
  29. var nopassUser = user;
  30. nopassUser.password = undefined;
  31.  
  32.  
  33. res.locals.mySession.user = user._id;
  34. res.locals.mySession.createdAt = Date.now();
  35. if (!res.locals.mySession.cache)
  36. res.locals.mySession.cache = {};
  37.  
  38. res.locals.mySession.cache.authenticated = true;
  39. res.locals.mySession.markModified("user");
  40. res.locals.mySession.markModified("cache");
  41. res.locals.mySession.save(HandleSessionSave);
  42.  
  43. res.locals.json.success = true;
  44. res.locals.json.user = nopassUser;
  45. }
  46. else
  47. {
  48. if (!loginAttempts[req.ip])
  49. loginAttempts[req.ip] = [];
  50. else
  51. loginAttempts[req.ip].push(Date.now());
  52.  
  53. if (user) {
  54. user.alerts.push({
  55. time: Date.now(),
  56. message: "There was a failed login attempt from: " + req.ip + ". There have been " + loginAttempts[req.ip] + " attempts to enter your account in the last hour (current time: " + new Date() + "). If the failed attempts continue the address " + req.ip + " will be blocked to protect your account.",
  57. viewed: false,
  58. from: "BITS",
  59. from_id: user._id,
  60. to_id: user._id,
  61. to: user.email
  62. });
  63.  
  64. user.markModified("additional_information");
  65. user.markModified("alerts");
  66.  
  67. user.save(function (err) {
  68. if (error.Handle(err)) {
  69. console.log("There was an error while attempting to handle a failed login.")
  70. }
  71. });
  72.  
  73. if (user.preferences.send_alert_emails && user.preferences.send_alert_emails.enable && user.preferences.send_alert_emails.email) {
  74. transporter.sendMail({
  75. from: 'bits@alltechnologysolutions.com',
  76. to: user.preferences.send_alert_emails.email,
  77. subject: 'BITS Alert: Failed Login Attempt',
  78. html: "There was a failed login attempt from: " + req.ip + ". There have been " + loginAttempts[req.ip] + " attempts to enter your account in the last hour (current time: " + new Date() + "). If the failed attempts continue the address " + req.ip + " will be blocked to protect your account."
  79.  
  80. }, function (err) {
  81. if (error.Handle(err))
  82. console.log("error sending mail: " + JSON.stringify(err));
  83. });
  84. }
  85. }
  86. res.locals.json.success = false;
  87. }
  88. next();
  89. });
  90. }
  91. else
  92. {
  93. res.locals.json.success = false;
  94. next();
  95. }
  96.  
  97.  
  98. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement