AuthGuard: add separate before and after registration callbacks

This commit is contained in:
Alice Gaudon 2020-11-04 11:55:34 +01:00
parent 19066b3e67
commit d741517cb9
2 changed files with 8 additions and 4 deletions

View File

@ -59,7 +59,8 @@ export default abstract class AuthGuard<P extends AuthProof<User>> {
session: Express.Session, session: Express.Session,
proof: P, proof: P,
onLogin?: (user: User) => Promise<void>, onLogin?: (user: User) => Promise<void>,
onRegister?: (connection: Connection, user: User) => Promise<RegisterCallback[]>, beforeRegister?: (connection: Connection, user: User) => Promise<RegisterCallback[]>,
afterRegister?: (connection: Connection, user: User) => Promise<RegisterCallback[]>,
): Promise<User> { ): Promise<User> {
if (!await proof.isValid()) throw new InvalidAuthProofError(); if (!await proof.isValid()) throw new InvalidAuthProofError();
if (!await proof.isAuthorized()) throw new UnauthorizedAuthProofError(); if (!await proof.isAuthorized()) throw new UnauthorizedAuthProofError();
@ -72,9 +73,12 @@ export default abstract class AuthGuard<P extends AuthProof<User>> {
user = await MysqlConnectionManager.wrapTransaction(async connection => { user = await MysqlConnectionManager.wrapTransaction(async connection => {
const user = User.create({}); const user = User.create({});
if (beforeRegister) {
(await beforeRegister(connection, user)).forEach(c => callbacks.push(c));
}
await user.save(connection, c => callbacks.push(c)); await user.save(connection, c => callbacks.push(c));
if (onRegister) { if (afterRegister) {
(await onRegister(connection, user)).forEach(c => callbacks.push(c)); (await afterRegister(connection, user)).forEach(c => callbacks.push(c));
} }
return user; return user;
}); });

View File

@ -23,7 +23,7 @@ export default abstract class MagicLinkAuthController extends AuthController {
// Auth // Auth
try { try {
return await req.as(AuthMiddleware).getAuthGuard().authenticateOrRegister( return await req.as(AuthMiddleware).getAuthGuard().authenticateOrRegister(
session, magicLink, undefined, async (connection, user) => { session, magicLink, undefined, undefined, async (connection, user) => {
const callbacks: RegisterCallback[] = []; const callbacks: RegisterCallback[] = [];
const userEmail = UserEmail.create({ const userEmail = UserEmail.create({