Skip to content

Commit 9945c3f

Browse files
authored
fix: remove old email on validation of new one (#619)
* fix: remove old email on validation of new one * fix: remove old email on validation of new one * fix: remove old email on validation of new one
1 parent 581edea commit 9945c3f

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/utils/contacts.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { HttpStatusError } = require('@microfleet/validation');
33
const challengeAct = require('./challenges/challenge');
44
const redisKey = require('./key');
55
const handlePipeline = require('./pipeline-error');
6-
const { getUserId } = require('./userData');
6+
const { getInternalData, getUserId } = require('./userData');
77
const {
88
USERS_CONTACTS,
99
USERS_DEFAULT_CONTACT,
@@ -64,8 +64,11 @@ async function removeAllEmailContactsOfUser(redisPipe, userId, exceptEmail) {
6464
}
6565
}
6666

67-
async function setUserName(redisPipe, userId, verifiedEmail) {
67+
async function replaceUserName(redisPipe, userId, verifiedEmail) {
6868
const { config: { jwt: { defaultAudience } } } = this;
69+
const internalData = await getInternalData.call(this, userId);
70+
const username = internalData[USERS_USERNAME_FIELD];
71+
redisPipe.hdel(USERS_USERNAME_TO_ID, username);
6972
redisPipe.hset(USERS_USERNAME_TO_ID, verifiedEmail, userId);
7073
redisPipe.hset(redisKey(userId, USERS_DATA), USERS_USERNAME_FIELD, verifiedEmail);
7174
redisPipe.hset(redisKey(userId, USERS_METADATA, defaultAudience), USERS_USERNAME_FIELD, JSON.stringify(verifiedEmail));
@@ -173,7 +176,7 @@ async function verifyEmail({ secret }) {
173176
await removeAllEmailContactsOfUser.call(this, pipe, userId, contact.value);
174177
}
175178
if (this.config.contacts.updateUsername) {
176-
await setUserName.call(this, pipe, userId, contact.value);
179+
await replaceUserName.call(this, pipe, userId, contact.value);
177180
}
178181
pipe.hset(key, 'verified', 'true');
179182
metadata.contact.verified = true;

test/suites/actions/contacts.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,9 @@ describe('#user contacts', function registerSuite() {
280280
.dispatch('contacts.verify-email', { params: { secret } });
281281

282282
// get meta of user by updated email
283-
const { [audience]: { username: updatedUserName } } = await this.users
283+
const { [audience]: { username: updatedUserName, id } } = await this.users
284284
.dispatch('getMetadata', { params: { username: params.contact.value, audience } });
285-
285+
this.testUser.id = id;
286286
assert.equal(updatedUserName, params.contact.value);
287287
assert.equal(value, params.contact.value);
288288
assert.strictEqual(verified, true);
@@ -292,7 +292,7 @@ describe('#user contacts', function registerSuite() {
292292

293293
it('should remove username to userid mapping on contact removal', async function test() {
294294
const params = {
295-
username: this.testUser.username,
295+
username: this.testUser.id,
296296
contact: {
297297
value: 'email@mail.org',
298298
type: 'email',

0 commit comments

Comments
 (0)