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/email.js

111 lines
3.0 KiB
JavaScript

'use strict';
var async = require('async'),
nconf = require('nconf'),
winston = require('winston'),
11 years ago
user = require('../user'),
utils = require('../../public/src/utils'),
translator = require('../../public/src/modules/translator'),
11 years ago
plugins = require('../plugins'),
db = require('../database'),
meta = require('../meta'),
emailer = require('../emailer');
(function(UserEmail) {
UserEmail.exists = function(email, callback) {
11 years ago
user.getUidByEmail(email.toLowerCase(), function(err, exists) {
callback(err, !!exists);
});
};
UserEmail.available = function(email, callback) {
db.isSortedSetMember('email:uid', email.toLowerCase(), function(err, exists) {
callback(err, !exists);
});
};
10 years ago
UserEmail.sendValidationEmail = function(uid, email, callback) {
10 years ago
callback = callback || function() {};
var confirm_code = utils.generateUUID(),
confirm_link = nconf.get('url') + '/confirm/' + confirm_code;
10 years ago
var emailInterval = 10;
10 years ago
10 years ago
async.waterfall([
function(next) {
db.get('uid:' + uid + ':confirm:email:sent', next);
},
function(sent, next) {
if (sent) {
return next(new Error('[[error:confirm-email-already-sent, ' + emailInterval + ']]'));
}
10 years ago
db.set('uid:' + uid + ':confirm:email:sent', 1, next);
},
function(next) {
db.pexpireAt('uid:' + uid + ':confirm:email:sent', Date.now() + (emailInterval * 60 * 1000), next);
},
function(next) {
plugins.fireHook('filter:user.verify.code', confirm_code, next);
},
function(_confirm_code, next) {
confirm_code = _confirm_code;
db.setObject('confirm:' + confirm_code, {
email: email.toLowerCase(),
uid: uid
}, next);
},
function(next) {
db.expireAt('confirm:' + confirm_code, Math.floor(Date.now() / 1000 + 60 * 60 * 24), next);
10 years ago
},
function(next) {
user.getUserField(uid, 'username', next);
},
function(username, next) {
var title = meta.config.title || meta.config.browserTitle || 'NodeBB';
translator.translate('[[email:welcome-to, ' + title + ']]', meta.config.defaultLang, function(subject) {
var data = {
site_title: title,
username: username,
confirm_link: confirm_link,
confirm_code: confirm_code,
10 years ago
subject: subject,
template: 'welcome',
uid: uid
};
10 years ago
if (plugins.hasListeners('action:user.verify')) {
plugins.fireHook('action:user.verify', {uid: uid, data: data});
next();
} else {
10 years ago
emailer.send('welcome', uid, data, next);
10 years ago
}
});
10 years ago
}
], callback);
};
UserEmail.confirm = function(code, callback) {
db.getObject('confirm:' + code, function(err, confirmObj) {
if (err) {
return callback(new Error('[[error:parse-error]]'));
}
if (confirmObj && confirmObj.uid && confirmObj.email) {
async.series([
async.apply(user.setUserField, confirmObj.uid, 'email:confirmed', 1),
async.apply(db.delete, 'confirm:' + code)
], function(err) {
callback(err ? new Error('[[error:email-confirm-failed]]') : null);
});
} else {
callback(new Error('[[error:invalid-data]]'));
}
});
};
}(exports));