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.ts33
1 files changed, 15 insertions, 18 deletions
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 @@
1import { GuardsList } from '@ioc:Adonis/Addons/Auth'; 1import { GuardsList } from '@ioc:Adonis/Addons/Auth'
2import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'; 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,27 +10,24 @@ 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( 15 protected async authenticate(auth: HttpContext['auth'], guards: (keyof GuardsList)[]) {
16 auth: HttpContextContract['auth'], 16 let guardLastAttempted: string | undefined
17 guards: (keyof GuardsList)[],
18 ) {
19 let guardLastAttempted: string | undefined;
20 17
21 for (const guard of guards) { 18 for (const guard of guards) {
22 guardLastAttempted = guard; 19 guardLastAttempted = guard
23 20
24 // eslint-disable-next-line no-await-in-loop 21 // eslint-disable-next-line no-await-in-loop
25 if (await auth.use(guard).check()) { 22 if (await auth.use(guard).check()) {
26 auth.defaultGuard = guard; 23 auth.defaultGuard = guard
27 24
28 throw new AuthenticationException( 25 throw new AuthenticationException(
29 'Unauthorized access', 26 'Unauthorized access',
30 'E_UNAUTHORIZED_ACCESS', 27 'E_UNAUTHORIZED_ACCESS',
31 guardLastAttempted, 28 guardLastAttempted,
32 this.redirectTo, 29 this.redirectTo
33 ); 30 )
34 } 31 }
35 } 32 }
36 } 33 }
@@ -39,18 +36,18 @@ export default class GuestMiddleware {
39 * Handle request 36 * Handle request
40 */ 37 */
41 public async handle( 38 public async handle(
42 { auth }: HttpContextContract, 39 { auth }: HttpContext,
43 next: () => Promise<void>, 40 next: () => Promise<void>,
44 customGuards: (keyof GuardsList)[], 41 customGuards: (keyof GuardsList)[]
45 ) { 42 ) {
46 /** 43 /**
47 * Uses the user defined guards or the default guard mentioned in 44 * Uses the user defined guards or the default guard mentioned in
48 * the config file 45 * the config file
49 */ 46 */
50 const guards = customGuards.length > 0 ? customGuards : [auth.name]; 47 const guards = customGuards.length > 0 ? customGuards : [auth.name]
51 48
52 await this.authenticate(auth, guards); 49 await this.authenticate(auth, guards)
53 50
54 await next(); 51 await next()
55 } 52 }
56} 53}