diff options
author | 2024-02-10 18:37:40 -0700 | |
---|---|---|
committer | 2024-02-13 06:59:44 +0530 | |
commit | e1c47572a6235fd8fd20af888ac3a11c7ae1369d (patch) | |
tree | 2dccff36a441916d7014037cef3f7ce84a790cad /app/Middleware/AllowGuestOnly.ts | |
parent | refactor: project maintenance (diff) | |
download | ferdium-server-e1c47572a6235fd8fd20af888ac3a11c7ae1369d.tar.gz ferdium-server-e1c47572a6235fd8fd20af888ac3a11c7ae1369d.tar.zst ferdium-server-e1c47572a6235fd8fd20af888ac3a11c7ae1369d.zip |
updates
Diffstat (limited to 'app/Middleware/AllowGuestOnly.ts')
-rw-r--r-- | app/Middleware/AllowGuestOnly.ts | 31 |
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 @@ | |||
1 | import { GuardsList } from '@ioc:Adonis/Addons/Auth' | 1 | import { GuardsList } from '@ioc:Adonis/Addons/Auth'; |
2 | import { HttpContext } from '@adonisjs/core/http' | 2 | import { HttpContext } from '@adonisjs/core/http'; |
3 | import { AuthenticationException } from '@adonisjs/auth/build/standalone' | 3 | import { 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 | } |