diff options
Diffstat (limited to 'app/Middleware/SilentAuth.ts')
-rw-r--r-- | app/Middleware/SilentAuth.ts | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/app/Middleware/SilentAuth.ts b/app/Middleware/SilentAuth.ts new file mode 100644 index 0000000..ee73ec4 --- /dev/null +++ b/app/Middleware/SilentAuth.ts | |||
@@ -0,0 +1,24 @@ | |||
1 | import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'; | ||
2 | |||
3 | /** | ||
4 | * Silent auth middleware can be used as a global middleware to silent check | ||
5 | * if the user is logged-in or not. | ||
6 | * | ||
7 | * The request continues as usual, even when the user is not logged-in. | ||
8 | */ | ||
9 | export default class SilentAuthMiddleware { | ||
10 | /** | ||
11 | * Handle request | ||
12 | */ | ||
13 | public async handle( | ||
14 | { auth }: HttpContextContract, | ||
15 | next: () => Promise<void>, | ||
16 | ) { | ||
17 | /** | ||
18 | * Check if user is logged-in or not. If yes, then `ctx.auth.user` will be | ||
19 | * set to the instance of the currently logged in user. | ||
20 | */ | ||
21 | await auth.check(); | ||
22 | await next(); | ||
23 | } | ||
24 | } | ||