From 88ca5f8d16a3136256c5d0bc7196b151db75d006 Mon Sep 17 00:00:00 2001 From: "Fon E. Noel NFEBE" Date: Mon, 21 Aug 2023 01:00:22 +0100 Subject: [PATCH] wip (fixup): Register unregistered users Signed-off-by: Fon E. Noel NFEBE --- src/classes/AuthenticationSession.ts | 51 +++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/src/classes/AuthenticationSession.ts b/src/classes/AuthenticationSession.ts index 666dae14..66ca073c 100644 --- a/src/classes/AuthenticationSession.ts +++ b/src/classes/AuthenticationSession.ts @@ -83,8 +83,7 @@ export class AuthenticationSession { password, }).then((clientResponse) => { switch (clientResponse.statusCode) { - case FusionAuthStatusCode.Success: - case FusionAuthStatusCode.SuccessButUnregisteredInApp: + case FusionAuthStatusCode.Success: { if (clientResponse.response.token !== undefined) { logger.verbose('Successful password authentication attempt.', { username: this.authContext.username, @@ -93,11 +92,22 @@ export class AuthenticationSession { this.authTokenExpiresAt = clientResponse.response.tokenExpirationInstant ?? 0; this.refreshToken = clientResponse.response.refreshToken ?? ''; this.authContext.accept(); - return; + } else { + this.authContext.reject(); } - this.authContext.reject(); return; - case FusionAuthStatusCode.SuccessNeedsTwoFactorAuth: + } + case FusionAuthStatusCode.SuccessButUnregisteredInApp: { + const userId: string = clientResponse.response.user?.id ?? ''; + this.registerUserInApp(userId) + .then(() => { this.processPasswordResponse([password]); }) + .catch((error) => { + logger.warn('Error during registration and authentication:', error); + this.authContext.reject(); + }); + return; + } + case FusionAuthStatusCode.SuccessNeedsTwoFactorAuth: { if (clientResponse.response.twoFactorId !== undefined) { logger.verbose('Successful password authentication attempt; MFA required.', { username: this.authContext.username, @@ -105,16 +115,18 @@ export class AuthenticationSession { this.twoFactorId = clientResponse.response.twoFactorId; this.twoFactorMethods = clientResponse.response.methods ?? []; this.promptForTwoFactorMethod(); - return; + } else { + this.authContext.reject(); } - this.authContext.reject(); return; - default: + } + default: { logger.verbose('Failed password authentication attempt.', { username: this.authContext.username, response: clientResponse.response, }); this.authContext.reject(); + } } }).catch((clientResponse: unknown) => { const message = isPartialClientResponse(clientResponse) @@ -125,6 +137,29 @@ export class AuthenticationSession { }); } + private async registerUserInApp(userId: string): Promise { + return this.fusionAuthClient.register(userId, { + registration: { + applicationId: this.fusionAuthAppId, + }, + }).then((clientResponse) => { + switch (clientResponse.statusCode) { + case FusionAuthStatusCode.Success: + logger.verbose('User registered successfully after authentication.', { + userId, + }); + break; + default: + logger.verbose('User registration after authentication failed.', { + userId, + response: clientResponse.response, + }); + } + }).catch((error) => { + logger.warn('Error during user registration after authentication:', error); + }); + } + private promptForTwoFactorMethod(): void { const promptOptions = this.twoFactorMethods.map( (method, index) => `[${index + 1}] ${method.method ?? ''}`,