From 7584d2d7a7110aef0331ebfa178b2295842c59fa Mon Sep 17 00:00:00 2001 From: MCMXC <16797721+mcmxcdev@users.noreply.github.com> Date: Sat, 10 Feb 2024 18:19:14 -0700 Subject: refactor: project maintenance - work in progress --- app/Middleware/AllowGuestOnly.ts | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) (limited to 'app/Middleware/AllowGuestOnly.ts') diff --git a/app/Middleware/AllowGuestOnly.ts b/app/Middleware/AllowGuestOnly.ts index ee43571..5ef5c34 100644 --- a/app/Middleware/AllowGuestOnly.ts +++ b/app/Middleware/AllowGuestOnly.ts @@ -1,6 +1,6 @@ -import { GuardsList } from '@ioc:Adonis/Addons/Auth'; -import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'; -import { AuthenticationException } from '@adonisjs/auth/build/standalone'; +import { GuardsList } from '@ioc:Adonis/Addons/Auth' +import { HttpContext } from '@adonisjs/core/http' +import { AuthenticationException } from '@adonisjs/auth/build/standalone' /** * This is actually a reverted a reverted auth middleware available in ./Auth.ts @@ -10,27 +10,24 @@ export default class GuestMiddleware { /** * The URL to redirect to when request is authorized */ - protected redirectTo = '/dashboard'; + protected redirectTo = '/dashboard' - protected async authenticate( - auth: HttpContextContract['auth'], - guards: (keyof GuardsList)[], - ) { - let guardLastAttempted: string | undefined; + protected async authenticate(auth: HttpContext['auth'], guards: (keyof GuardsList)[]) { + let guardLastAttempted: string | undefined for (const guard of guards) { - guardLastAttempted = guard; + guardLastAttempted = guard // eslint-disable-next-line no-await-in-loop if (await auth.use(guard).check()) { - auth.defaultGuard = guard; + auth.defaultGuard = guard throw new AuthenticationException( 'Unauthorized access', 'E_UNAUTHORIZED_ACCESS', guardLastAttempted, - this.redirectTo, - ); + this.redirectTo + ) } } } @@ -39,18 +36,18 @@ export default class GuestMiddleware { * Handle request */ public async handle( - { auth }: HttpContextContract, + { auth }: HttpContext, next: () => Promise, - customGuards: (keyof GuardsList)[], + customGuards: (keyof GuardsList)[] ) { /** * Uses the user defined guards or the default guard mentioned in * the config file */ - const guards = customGuards.length > 0 ? customGuards : [auth.name]; + const guards = customGuards.length > 0 ? customGuards : [auth.name] - await this.authenticate(auth, guards); + await this.authenticate(auth, guards) - await next(); + await next() } } -- cgit v1.2.3-70-g09d2