Advertisement
Guest User

Untitled

a guest
Dec 14th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. preLoginHandler: function(formData, req, res, next) {
  2. var authRequest = {
  3. username: formData.login,
  4. password: formData.password
  5. };
  6.  
  7. //Try to authenticate the user
  8. req.app.get('stormpathApplication').authenticateAccount(authRequest, function(err, result) {
  9. //if (err) return helpers.handleError(err,res);
  10. if (err && err.code == 7100) {
  11. //7100 means the password was wrong, email correct
  12. //Find the user by email address
  13. req.app.get('stormpathApplication').getAccounts({
  14. email: formData.login
  15. }, function(err, results) {
  16. if (results && results.size > 0) {
  17. var account = results.items[0];
  18. var loginAttempt = {};
  19.  
  20. //Setup sessions custom data, if not exists yet
  21. if (!account.customData.loginAttempts) account.customData.loginAttempts = [];
  22.  
  23. //Log ip address
  24. loginAttempt.ip = req.headers['x-forwarded-for'] ||
  25. req.connection.remoteAddress ||
  26. req.socket.remoteAddress ||
  27. req.connection.socket.remoteAddress;
  28.  
  29. //Log user agent
  30. loginAttempt.userAgent = req.headers['user-agent'];
  31.  
  32. //Log time
  33. loginAttempt.time = Date.now();
  34.  
  35. //Save customData
  36. account.customData.loginAttempts.push(loginAttempt);
  37. account.customData.save();
  38.  
  39. //Send notification email
  40. mailer.sendEmailTemplate(account.email, 'loginAttemptEmail', loginAttempt);
  41. }
  42. });
  43.  
  44. }
  45. });
  46.  
  47. next();
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement