From 41a083ba52348ef781fb9ebaff5eaf92793b69cb Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Sun, 6 Mar 2022 16:34:57 +0100 Subject: [PATCH] feat(back/auth): add use() method to AuthProof and call it on successful login attempt --- src/auth/AuthGuard.ts | 3 +++ src/auth/AuthProof.ts | 6 ++++++ src/auth/magic_link/MagicLinkController.ts | 2 +- src/auth/models/MagicLink.ts | 2 +- 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/auth/AuthGuard.ts b/src/auth/AuthGuard.ts index 0ea3827..c24fde9 100644 --- a/src/auth/AuthGuard.ts +++ b/src/auth/AuthGuard.ts @@ -160,6 +160,9 @@ export default class AuthGuard { throw new PendingApprovalAuthError(); } + // Mark auth proof as used + await proof.use?.(); + // Login session.isAuthenticated = true; session.persistent = persistSession; diff --git a/src/auth/AuthProof.ts b/src/auth/AuthProof.ts index 960d119..e868f95 100644 --- a/src/auth/AuthProof.ts +++ b/src/auth/AuthProof.ts @@ -38,4 +38,10 @@ export default interface AuthProof { * instance. */ revoke(): Promise; + + /** + * This method is called when the AuthProof was used in a successful login attempt. + * If you modify the AuthProof, you should make sure changes are persistent. + */ + use?(): Promise; } diff --git a/src/auth/magic_link/MagicLinkController.ts b/src/auth/magic_link/MagicLinkController.ts index 8639e05..402662f 100644 --- a/src/auth/magic_link/MagicLinkController.ts +++ b/src/auth/magic_link/MagicLinkController.ts @@ -141,7 +141,7 @@ export default class MagicLinkController extends Controll } if (await validLink.isAuthorized()) { - validLink.use(); + validLink.useLink(); await validLink.save(); await this.performAction(validLink, req, res); return; diff --git a/src/auth/models/MagicLink.ts b/src/auth/models/MagicLink.ts index b9c5d1b..34845be 100644 --- a/src/auth/models/MagicLink.ts +++ b/src/auth/models/MagicLink.ts @@ -62,7 +62,7 @@ export default class MagicLink extends Model implements AuthProof { return this.used; } - public use(): void { + public useLink(): void { this.used = true; }