You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/user/reset.js

90 lines
2.2 KiB
JavaScript

'use strict';
var async = require('async'),
nconf = require('nconf'),
11 years ago
user = require('../user'),
utils = require('../../public/src/utils'),
translator = require('../../public/src/translator'),
11 years ago
db = require('../database'),
meta = require('../meta'),
events = require('../events'),
emailer = require('../emailer');
(function(UserReset) {
UserReset.validate = function(code, callback) {
db.getObjectField('reset:uid', code, function(err, uid) {
if (err || !uid) {
return callback(err, false);
}
10 years ago
db.sortedSetScore('reset:issueDate', code, function(err, issueDate) {
// db.getObjectField('reset:expiry', code, function(err, expiry) {
if (err) {
return callback(err);
}
10 years ago
callback(null, parseInt(issueDate, 10) > (Date.now() - (1000*60*120)));
});
});
};
UserReset.send = function(email, callback) {
user.getUidByEmail(email, function(err, uid) {
11 years ago
if (err || !uid) {
return callback(err || new Error('[[error:invalid-email]]'));
}
var reset_code = utils.generateUUID();
db.setObjectField('reset:uid', reset_code, uid);
10 years ago
db.sortedSetAdd('reset:issueDate', Date.now(), reset_code);
var reset_link = nconf.get('url') + '/reset/' + reset_code;
11 years ago
translator.translate('[[email:password-reset-requested, ' + (meta.config.title || 'NodeBB') + ']]', meta.config.defaultLang, function(subject) {
emailer.send('reset', uid, {
site_title: (meta.config.title || 'NodeBB'),
reset_link: reset_link,
subject: subject,
template: 'reset',
uid: uid
});
callback();
});
});
};
UserReset.commit = function(code, password, callback) {
UserReset.validate(code, function(err, validated) {
if(err) {
return callback(err);
}
11 years ago
if (!validated) {
return;
}
db.getObjectField('reset:uid', code, function(err, uid) {
if (err) {
return callback(err);
}
user.hashPassword(password, function(err, hash) {
if (err) {
return callback(err);
}
11 years ago
user.setUserField(uid, 'password', hash);
db.deleteObjectField('reset:uid', code);
10 years ago
db.sortedSetRemove('reset:issueDate', code);
11 years ago
user.auth.resetLockout(uid, callback);
});
11 years ago
});
});
};
}(exports));