Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- preLoginHandler: function(formData, req, res, next) {
- var authRequest = {
- username: formData.login,
- password: formData.password
- };
- //Try to authenticate the user
- req.app.get('stormpathApplication').authenticateAccount(authRequest, function(err, result) {
- //if (err) return helpers.handleError(err,res);
- if (err && err.code == 7100) {
- //7100 means the password was wrong, email correct
- //Find the user by email address
- req.app.get('stormpathApplication').getAccounts({
- email: formData.login
- }, function(err, results) {
- if (results && results.size > 0) {
- var account = results.items[0];
- var loginAttempt = {};
- //Setup sessions custom data, if not exists yet
- if (!account.customData.loginAttempts) account.customData.loginAttempts = [];
- //Log ip address
- loginAttempt.ip = req.headers['x-forwarded-for'] ||
- req.connection.remoteAddress ||
- req.socket.remoteAddress ||
- req.connection.socket.remoteAddress;
- //Log user agent
- loginAttempt.userAgent = req.headers['user-agent'];
- //Log time
- loginAttempt.time = Date.now();
- //Save customData
- account.customData.loginAttempts.push(loginAttempt);
- account.customData.save();
- //Send notification email
- mailer.sendEmailTemplate(account.email, 'loginAttemptEmail', loginAttempt);
- }
- });
- }
- });
- next();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement