From d741517cb94775685dc805c24e9d96b85c1d4c91 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Wed, 4 Nov 2020 11:55:34 +0100 Subject: [PATCH] AuthGuard: add separate before and after registration callbacks --- src/auth/AuthGuard.ts | 10 +++++++--- src/auth/magic_link/MagicLinkAuthController.ts | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/auth/AuthGuard.ts b/src/auth/AuthGuard.ts index 4d645a3..9db2ddc 100644 --- a/src/auth/AuthGuard.ts +++ b/src/auth/AuthGuard.ts @@ -59,7 +59,8 @@ export default abstract class AuthGuard

> { session: Express.Session, proof: P, onLogin?: (user: User) => Promise, - onRegister?: (connection: Connection, user: User) => Promise, + beforeRegister?: (connection: Connection, user: User) => Promise, + afterRegister?: (connection: Connection, user: User) => Promise, ): Promise { if (!await proof.isValid()) throw new InvalidAuthProofError(); if (!await proof.isAuthorized()) throw new UnauthorizedAuthProofError(); @@ -72,9 +73,12 @@ export default abstract class AuthGuard

> { user = await MysqlConnectionManager.wrapTransaction(async connection => { const user = User.create({}); + if (beforeRegister) { + (await beforeRegister(connection, user)).forEach(c => callbacks.push(c)); + } await user.save(connection, c => callbacks.push(c)); - if (onRegister) { - (await onRegister(connection, user)).forEach(c => callbacks.push(c)); + if (afterRegister) { + (await afterRegister(connection, user)).forEach(c => callbacks.push(c)); } return user; }); diff --git a/src/auth/magic_link/MagicLinkAuthController.ts b/src/auth/magic_link/MagicLinkAuthController.ts index 7d44f4f..ea988d9 100644 --- a/src/auth/magic_link/MagicLinkAuthController.ts +++ b/src/auth/magic_link/MagicLinkAuthController.ts @@ -23,7 +23,7 @@ export default abstract class MagicLinkAuthController extends AuthController { // Auth try { return await req.as(AuthMiddleware).getAuthGuard().authenticateOrRegister( - session, magicLink, undefined, async (connection, user) => { + session, magicLink, undefined, undefined, async (connection, user) => { const callbacks: RegisterCallback[] = []; const userEmail = UserEmail.create({