From f410ceb7058506d8747e4c4a6d98f81b5116c25e Mon Sep 17 00:00:00 2001 From: Miguel <113119956+mikeplascdev@users.noreply.github.com> Date: Wed, 29 May 2024 10:47:24 -0600 Subject: [PATCH] fix(magic-link): delete all previous login token by provider before create the new one (#10269) --- .../src/user/magic-link/magic-link.class.ts | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/packages/server-core/src/user/magic-link/magic-link.class.ts b/packages/server-core/src/user/magic-link/magic-link.class.ts index 2dce59975b..50c24f5cbd 100755 --- a/packages/server-core/src/user/magic-link/magic-link.class.ts +++ b/packages/server-core/src/user/magic-link/magic-link.class.ts @@ -164,12 +164,11 @@ export class MagicLinkService implements ServiceInterface { } if (identityProvider) { + await this.removePreviousLoginTokensByProvider(identityProvider.id) const loginToken = await this.app.service(loginTokenPath).create({ identityProviderId: identityProvider.id }) - await this.removePreviousLoginTokens(identityProvider.id) - if (data.type === 'email') { await this.sendEmail(data.email, loginToken.token) } else if (data.type === 'sms') { @@ -179,19 +178,12 @@ export class MagicLinkService implements ServiceInterface { return data } - private async removePreviousLoginTokens(identityProviderId: string) { + private async removePreviousLoginTokensByProvider(identityProviderId: string) { const loginTokenService = this.app.service(loginTokenPath) - const previousTokens = await loginTokenService.find({ + await loginTokenService.remove(null, { query: { - identityProviderId: identityProviderId + identityProviderId } }) - // Keep only the latest token and remove the rest - if (previousTokens.total > 1) { - const tokensToRemove = previousTokens.data.slice(1) - for (const token of tokensToRemove) { - await loginTokenService.remove(token.id) - } - } } }