AuthGuard: add separate before and after registration callbacks
This commit is contained in:
parent
19066b3e67
commit
d741517cb9
@ -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;
|
||||||
});
|
});
|
||||||
|
@ -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({
|
||||||
|
Loading…
Reference in New Issue
Block a user