diff --git a/src/auth/AuthGuard.ts b/src/auth/AuthGuard.ts index 15e5a46..a808a87 100644 --- a/src/auth/AuthGuard.ts +++ b/src/auth/AuthGuard.ts @@ -12,7 +12,7 @@ export default abstract class AuthGuard

{ return await User.getById(session.auth_id); } - public async authenticateOrRegister(session: Express.Session, proof: P): Promise { + public async authenticateOrRegister(session: Express.Session, proof: P, registerCallback?: (connection: Connection, userID: number) => Promise<(() => Promise)[]>): Promise { if (!await proof.isAuthorized()) { throw new AuthError('Invalid argument: cannot authenticate with an unauthorized proof.'); } @@ -34,6 +34,9 @@ export default abstract class AuthGuard

{ main: true, }); await userEmail.save(connection, c => callbacks.push(c)); + if (registerCallback) { + (await registerCallback(connection, user.id!)).forEach(c => callbacks.push(c)); + } }); for (const callback of callbacks) { @@ -43,6 +46,8 @@ export default abstract class AuthGuard

{ if (!user) { throw new Error('Unable to register user.'); } + } else if (registerCallback) { + throw new AuthError('User already exists.'); } session.auth_id = user.id;