@@ -3,7 +3,7 @@ const { HttpStatusError } = require('@microfleet/validation');
33const challengeAct = require ( './challenges/challenge' ) ;
44const redisKey = require ( './key' ) ;
55const handlePipeline = require ( './pipeline-error' ) ;
6- const { getUserId } = require ( './userData' ) ;
6+ const { getInternalData , getUserId } = require ( './userData' ) ;
77const {
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 ;
0 commit comments