From 949925586f414cb54dc4aa0621174b4ce1f2c838 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Thu, 3 May 2018 17:50:53 -0600 Subject: [PATCH] handle confirmed email better --- .../talk-plugin-local-auth/client/graphql.js | 34 +++++++++++++++++++ .../talk-plugin-local-auth/client/index.js | 2 ++ .../talk-plugin-local-auth/server/mutators.js | 1 + services/users.js | 5 ++- 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 plugins/talk-plugin-local-auth/client/graphql.js diff --git a/plugins/talk-plugin-local-auth/client/graphql.js b/plugins/talk-plugin-local-auth/client/graphql.js new file mode 100644 index 000000000..67fde246a --- /dev/null +++ b/plugins/talk-plugin-local-auth/client/graphql.js @@ -0,0 +1,34 @@ +import update from 'immutability-helper'; +import get from 'lodash/get'; + +export default { + mutations: { + UpdateEmailAddress: () => ({ + updateQueries: { + CoralEmbedStream_Profile: previousData => { + // Find the local profile (if they have one). + const localIndex = get(previousData, 'me.profiles', []).indexOf( + ({ provider }) => provider === 'local' + ); + if (localIndex < 0) { + return previousData; + } + + // Mutate the confirmedAt, because we changed the email address, they + // can't possibly be confirmed now as well. + return update(previousData, { + me: { + profiles: { + [localIndex]: { + confirmedAt: { + $set: null, + }, + }, + }, + }, + }); + }, + }, + }), + }, +}; diff --git a/plugins/talk-plugin-local-auth/client/index.js b/plugins/talk-plugin-local-auth/client/index.js index c669effaa..d5f9f8636 100644 --- a/plugins/talk-plugin-local-auth/client/index.js +++ b/plugins/talk-plugin-local-auth/client/index.js @@ -1,6 +1,7 @@ import ChangePassword from './containers/ChangePassword'; import Profile from './containers/Profile'; import translations from './translations.yml'; +import graphql from './graphql'; export default { translations, @@ -8,4 +9,5 @@ export default { profileHeader: [Profile], profileSettings: [ChangePassword], }, + ...graphql, }; diff --git a/plugins/talk-plugin-local-auth/server/mutators.js b/plugins/talk-plugin-local-auth/server/mutators.js index cb34bed7a..6798584f5 100644 --- a/plugins/talk-plugin-local-auth/server/mutators.js +++ b/plugins/talk-plugin-local-auth/server/mutators.js @@ -43,6 +43,7 @@ async function updateUserEmailAddress(ctx, email, confirmPassword) { }, { $set: { 'profiles.$.id': email }, + $unset: { 'profiles.$.metadata.confirmed_at': 1 }, } ); diff --git a/services/users.js b/services/users.js index 1e84c8ff1..d5486b3f8 100644 --- a/services/users.js +++ b/services/users.js @@ -32,6 +32,7 @@ const i18n = require('./i18n'); const Wordlist = require('./wordlist'); const DomainList = require('./domain_list'); const Limit = require('./limit'); +const { get } = require('lodash'); const EMAIL_CONFIRM_JWT_SUBJECT = 'email_confirm'; const PASSWORD_RESET_JWT_SUBJECT = 'password_reset'; @@ -965,7 +966,9 @@ class Users { throw new ErrNotFound(); } - if (profile.metadata && profile.metadata.confirmed_at !== null) { + // Check to see if the profile has already been confirmed. + const confirmedAt = get(profile, 'metadata.confirmed_at', null); + if (confirmedAt && confirmedAt < Date.now()) { throw new ErrEmailAlreadyVerified(); }