aboutsummaryrefslogtreecommitdiffstats
path: root/app/Middleware/AllowGuestOnly.ts
diff options
context:
space:
mode:
Diffstat (limited to 'app/Middleware/AllowGuestOnly.ts')
-rw-r--r--app/Middleware/AllowGuestOnly.ts31
1 files changed, 17 insertions, 14 deletions
diff --git a/app/Middleware/AllowGuestOnly.ts b/app/Middleware/AllowGuestOnly.ts
index 5ef5c34..75bf269 100644
--- a/app/Middleware/AllowGuestOnly.ts
+++ b/app/Middleware/AllowGuestOnly.ts
@@ -1,6 +1,6 @@
1import { GuardsList } from '@ioc:Adonis/Addons/Auth' 1import { GuardsList } from '@ioc:Adonis/Addons/Auth';
2import { HttpContext } from '@adonisjs/core/http' 2import { HttpContext } from '@adonisjs/core/http';
3import { AuthenticationException } from '@adonisjs/auth/build/standalone' 3import { AuthenticationException } from '@adonisjs/auth/build/standalone';
4 4
5/** 5/**
6 * This is actually a reverted a reverted auth middleware available in ./Auth.ts 6 * This is actually a reverted a reverted auth middleware available in ./Auth.ts
@@ -10,24 +10,27 @@ export default class GuestMiddleware {
10 /** 10 /**
11 * The URL to redirect to when request is authorized 11 * The URL to redirect to when request is authorized
12 */ 12 */
13 protected redirectTo = '/dashboard' 13 protected redirectTo = '/dashboard';
14 14
15 protected async authenticate(auth: HttpContext['auth'], guards: (keyof GuardsList)[]) { 15 protected async authenticate(
16 let guardLastAttempted: string | undefined 16 auth: HttpContext['auth'],
17 guards: (keyof GuardsList)[],
18 ) {
19 let guardLastAttempted: string | undefined;
17 20
18 for (const guard of guards) { 21 for (const guard of guards) {
19 guardLastAttempted = guard 22 guardLastAttempted = guard;
20 23
21 // eslint-disable-next-line no-await-in-loop 24 // eslint-disable-next-line no-await-in-loop
22 if (await auth.use(guard).check()) { 25 if (await auth.use(guard).check()) {
23 auth.defaultGuard = guard 26 auth.defaultGuard = guard;
24 27
25 throw new AuthenticationException( 28 throw new AuthenticationException(
26 'Unauthorized access', 29 'Unauthorized access',
27 'E_UNAUTHORIZED_ACCESS', 30 'E_UNAUTHORIZED_ACCESS',
28 guardLastAttempted, 31 guardLastAttempted,
29 this.redirectTo 32 this.redirectTo,
30 ) 33 );
31 } 34 }
32 } 35 }
33 } 36 }
@@ -38,16 +41,16 @@ export default class GuestMiddleware {
38 public async handle( 41 public async handle(
39 { auth }: HttpContext, 42 { auth }: HttpContext,
40 next: () => Promise<void>, 43 next: () => Promise<void>,
41 customGuards: (keyof GuardsList)[] 44 customGuards: (keyof GuardsList)[],
42 ) { 45 ) {
43 /** 46 /**
44 * Uses the user defined guards or the default guard mentioned in 47 * Uses the user defined guards or the default guard mentioned in
45 * the config file 48 * the config file
46 */ 49 */
47 const guards = customGuards.length > 0 ? customGuards : [auth.name] 50 const guards = customGuards.length > 0 ? customGuards : [auth.name];
48 51
49 await this.authenticate(auth, guards) 52 await this.authenticate(auth, guards);
50 53
51 await next() 54 await next();
52 } 55 }
53} 56}