added email fixes

This commit is contained in:
Wyatt Johnson
2018-01-11 10:58:43 -07:00
parent 92b506c85a
commit 1dcf297a45
5 changed files with 42 additions and 28 deletions
+4 -9
View File
@@ -105,8 +105,7 @@ function printUserAsTable(user) {
table.push( table.push(
{'ID': user.id.gray}, {'ID': user.id.gray},
{'Username': user.username}, {'Username': user.username},
{'Emails': user.profiles.filter(({provider}) => provider === 'local').map(({id})=> id) {'Emails': user.emails},
.join(', ')},
{'Tags': user.tags ? user.tags.map(({tag: {name}}) => name) : ''}, {'Tags': user.tags ? user.tags.map(({tag: {name}}) => name) : ''},
{'Role': user.role}, {'Role': user.role},
{'Verified': user.hasVerifiedEmail}, {'Verified': user.hasVerifiedEmail},
@@ -161,11 +160,7 @@ async function searchUsers() {
} }
return data.users.nodes.map((user) => { return data.users.nodes.map((user) => {
const emails = user.profiles const emails = user.emails.join(', ');
.filter(({provider}) => provider === 'local')
.map(({id})=> id)
.join(', ');
return { return {
name: `${user.username} (${emails}) ${user.id.gray} - ${user.role.gray}`, name: `${user.username} (${emails}) ${user.id.gray} - ${user.role.gray}`,
value: user.id, value: user.id,
@@ -230,8 +225,8 @@ async function verifyUserEmail(userID, email) {
} }
// Get all the user's email addresses. // Get all the user's email addresses.
const emails = user.profiles.filter(({provider}) => provider === 'local').map(({id}) => id); const emails = user.emails;
if (!emails || emails.length === 0) { if (emails.length === 0) {
throw new Error('user did not have any email addresses'); throw new Error('user did not have any email addresses');
} }
+21
View File
@@ -279,6 +279,27 @@ UserSchema.method('can', function(...actions) {
return can(this, ...actions); return can(this, ...actions);
}); });
/**
* firstEmail will return the first email on the user.
*/
UserSchema.virtual('firstEmail').get(function() {
const emails = this.emails;
if (emails.length === 0) {
return null;
}
return emails[0];
});
/**
* emails will return all the emails on a user.
*/
UserSchema.virtual('emails').get(function() {
return (this.profiles || [])
.filter(({provider}) => provider === 'local')
.map(({id}) => id);
});
/** /**
* hasVerifiedEmail will return true if at least one of the local email accounts * hasVerifiedEmail will return true if at least one of the local email accounts
* have their email verified. * have their email verified.
+3 -3
View File
@@ -79,13 +79,13 @@ router.post('/:user_id/email/confirm', authorization.needed('ADMIN', 'MODERATOR'
} }
// Find the first local profile. // Find the first local profile.
let localProfile = user.profiles.find((profile) => profile.provider === 'local'); const email = user.firstEmail;
if (!localProfile) { if (!email) {
return next(errors.ErrMissingEmail); return next(errors.ErrMissingEmail);
} }
// Send the email to the first local profile that was found. // Send the email to the first local profile that was found.
await UsersService.sendEmailConfirmation(user, localProfile.id); await UsersService.sendEmailConfirmation(user, email);
res.status(204).end(); res.status(204).end();
} catch (e) { } catch (e) {
+4 -3
View File
@@ -11,6 +11,7 @@ const uuid = require('uuid');
const debug = require('debug')('talk:services:passport'); const debug = require('debug')('talk:services:passport');
const bowser = require('bowser'); const bowser = require('bowser');
const ms = require('ms'); const ms = require('ms');
const _ = require('lodash');
// Create a redis client to use for authentication. // Create a redis client to use for authentication.
const {createClientFactory} = require('./redis'); const {createClientFactory} = require('./redis');
@@ -144,7 +145,7 @@ async function ValidateUserLogin(loginProfile, user, done) {
// If the profile doesn't have a metadata field, or it does not have a // If the profile doesn't have a metadata field, or it does not have a
// confirmed_at field, or that field is null, then send them back. // confirmed_at field, or that field is null, then send them back.
if (!profile.metadata || !profile.metadata.confirmed_at || profile.metadata.confirmed_at === null) { if (_.get(profile, 'metadata.confirmed_at', null) === null) {
return done(errors.ErrNotVerified); return done(errors.ErrNotVerified);
} }
} }
@@ -318,7 +319,7 @@ const CheckIfNeedsRecaptcha = (user, email) => {
throw new Error('ID indicated by loginProfile is not on user object'); throw new Error('ID indicated by loginProfile is not on user object');
} }
if (profile.metadata && profile.metadata.recaptcha_required) { if (_.get(profile, 'metadata.recaptcha_required', false)) {
return true; return true;
} }
@@ -501,7 +502,7 @@ passport.use(new LocalStrategy({
} }
// Define the loginProfile being used to perform an additional // Define the loginProfile being used to perform an additional
// verificaiton. // verification.
let loginProfile = {id: email, provider: 'local'}; let loginProfile = {id: email, provider: 'local'};
// Perform final steps to login the user. // Perform final steps to login the user.
+10 -13
View File
@@ -339,7 +339,7 @@ class UsersService {
} }
/** /**
* Sets or unsets the recaptcha_required flag on a user's local profile. * Sets or removes the recaptcha_required flag on a user's local profile.
*/ */
static flagForRecaptchaRequirement(email, required) { static flagForRecaptchaRequirement(email, required) {
return UserModel.update( return UserModel.update(
@@ -436,20 +436,17 @@ class UsersService {
} }
static async sendEmail(user, options) { static async sendEmail(user, options) {
const localProfile = user.profiles.find( const email = user.firstEmail;
(profile) => profile.provider === 'local' if (!email) {
);
if (!localProfile) { // Rather than throwing an error here, we'll
throw new Error('user does not have an email'); console.warn(new Error('user does not have an email'));
return;
} }
const {id: to} = localProfile; return mailer.send(merge({}, options, {
to: email,
options = merge(options, { }));
to,
});
return mailer.send(options);
} }
static async changePassword(id, password) { static async changePassword(id, password) {