From b83f1feff6d88c4f367b6c38f1d10483d2a560f6 Mon Sep 17 00:00:00 2001 From: Michael Bromley Date: Thu, 16 Jul 2020 13:13:41 +0200 Subject: [PATCH] feat(core): Include auth strategy name in AttemptedLoginEvent BREAKING CHANGE: The `AttemptedLoginEvent.identifier` property is now optional, since it will only be sent when using the "native" authentication strategy. Code that listens for this event should now check that the `identifier` property is defined before attempting to use it. --- .../src/event-bus/events/attempted-login-event.ts | 4 +++- packages/core/src/service/services/auth.service.ts | 11 ++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/core/src/event-bus/events/attempted-login-event.ts b/packages/core/src/event-bus/events/attempted-login-event.ts index d169b6e7ad..222c5a5f05 100644 --- a/packages/core/src/event-bus/events/attempted-login-event.ts +++ b/packages/core/src/event-bus/events/attempted-login-event.ts @@ -5,12 +5,14 @@ import { VendureEvent } from '../vendure-event'; /** * @description * This event is fired when an attempt is made to log in via the shop or admin API `login` mutation. + * The `strategy` represents the name of the AuthenticationStrategy used in the login attempt. + * If the "native" strategy is used, the additional `identifier` property will be available. * * @docsCategory events * @docsPage Event Types */ export class AttemptedLoginEvent extends VendureEvent { - constructor(public ctx: RequestContext, public identifier: string) { + constructor(public ctx: RequestContext, public strategy: string, public identifier?: string) { super(); } } diff --git a/packages/core/src/service/services/auth.service.ts b/packages/core/src/service/services/auth.service.ts index c620d5d7f1..39c1f84cf0 100644 --- a/packages/core/src/service/services/auth.service.ts +++ b/packages/core/src/service/services/auth.service.ts @@ -9,6 +9,7 @@ import { InternalServerError, NotVerifiedError, UnauthorizedError } from '../../ import { AuthenticationStrategy } from '../../config/auth/authentication-strategy'; import { NATIVE_AUTH_STRATEGY_NAME, + NativeAuthenticationData, NativeAuthenticationStrategy, } from '../../config/auth/native-authentication-strategy'; import { ConfigService } from '../../config/config.service'; @@ -42,7 +43,15 @@ export class AuthService { authenticationMethod: string, authenticationData: any, ): Promise { - this.eventBus.publish(new AttemptedLoginEvent(ctx, authenticationMethod)); + this.eventBus.publish( + new AttemptedLoginEvent( + ctx, + authenticationMethod, + authenticationMethod === NATIVE_AUTH_STRATEGY_NAME + ? (authenticationData as NativeAuthenticationData).username + : undefined, + ), + ); const authenticationStrategy = this.getAuthenticationStrategy(apiType, authenticationMethod); const user = await authenticationStrategy.authenticate(ctx, authenticationData); if (!user) {