Skip to content

Commit

Permalink
cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattjoh committed Apr 20, 2018
1 parent 7fa10b1 commit 7d64411
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
25 changes: 9 additions & 16 deletions plugins/talk-plugin-auth/server/mutators.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
const {
ErrNotAuthorized,
ErrPasswordTooShort,
ErrNotFound,
ErrEmailTaken,
} = require('errors');
const { ErrNotAuthorized, ErrNotFound, ErrEmailTaken } = require('errors');
const { ErrNoLocalProfile, ErrLocalProfile } = require('./errors');
const { get } = require('lodash');
const bcrypt = require('bcryptjs');

function hasLocalProfile(user) {
return Boolean(
get(user, 'profiles', []).find(({ provider }) => provider === 'local')
);
}
// hasLocalProfile checks a user's profiles to see if they already have a local
// profile associated with their account.
const hasLocalProfile = user =>
get(user, 'profiles', []).some(({ provider }) => provider === 'local');

// updateUserEmailAddress will verify that the user has sent the correct
// password followed by executing the email change and notifying the emails
Expand Down Expand Up @@ -60,7 +54,7 @@ async function updateUserEmailAddress(ctx, email, confirmPassword) {
locals: {
body: I18n.t(
'email.email_change_original.body',
user.email,
user.firstEmail,
email,
organizationContactEmail
),
Expand All @@ -87,9 +81,7 @@ async function attachUserLocalAuth(ctx, email, password) {
email = email.toLowerCase().trim();

// Validate the password.
if (!password || password.length < 8) {
throw new ErrPasswordTooShort();
}
await Users.isValidPassword(password);

// Hash the new password.
const hashedPassword = await bcrypt.hash(password, 10);
Expand All @@ -116,10 +108,11 @@ async function attachUserLocalAuth(ctx, email, password) {
);
if (!updatedUser) {
const foundUser = await User.findOne({ id: user.id });
if (foundUser === null) {
if (!foundUser) {
throw new ErrNotFound();
}

// Check to see if this was the result of a race.
if (hasLocalProfile(foundUser)) {
throw new ErrLocalProfile();
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/talk-plugin-auth/server/translations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ en:
email:
email_change_original:
subject: Email change
body: Your email address has been changed from {0} to {1}. If you did not initiate this change, please contact {2}.
body: Your email address has been changed from {0} to {1}. If you did not initiate this change, please contact {2}. # TODO: update translation
error:
NO_LOCAL_PROFILE: No existing email address is associated with this account.
LOCAL_PROFILE: An email address is already associated with this account.

0 comments on commit 7d64411

Please sign in to comment.