mirror of
https://github.com/sergeyyarkov/educt-server.git
synced 2026-10-11 10:19:20 +03:00
73 lines
2.3 KiB
TypeScript
73 lines
2.3 KiB
TypeScript
import { GuardsList } from '@ioc:Adonis/Addons/Auth';
|
|
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext';
|
|
import { AuthenticationException } from '@adonisjs/auth/build/standalone';
|
|
|
|
/**
|
|
* Auth middleware is meant to restrict un-authenticated access to a given route
|
|
* or a group of routes.
|
|
*
|
|
* You must register this middleware inside `start/kernel.ts` file under the list
|
|
* of named middleware.
|
|
*/
|
|
export default class AuthMiddleware {
|
|
/**
|
|
* The URL to redirect to when request is Unauthorized
|
|
*/
|
|
protected redirectTo = '/login';
|
|
|
|
/**
|
|
* Authenticates the current HTTP request against a custom set of defined
|
|
* guards.
|
|
*
|
|
* The authentication loop stops as soon as the user is authenticated using any
|
|
* of the mentioned guards and that guard will be used by the rest of the code
|
|
* during the current request.
|
|
*/
|
|
protected async authenticate(auth: HttpContextContract['auth'], guards: (keyof GuardsList)[]) {
|
|
/**
|
|
* Hold reference to the guard last attempted within the for loop. We pass
|
|
* the reference of the guard to the "AuthenticationException", so that
|
|
* it can decide the correct response behavior based upon the guard
|
|
* driver
|
|
*/
|
|
let guardLastAttempted: string | undefined;
|
|
const authPromises: Promise<boolean>[] = guards.map(guard => auth.use(guard).check());
|
|
const authResults: boolean[] = await Promise.all(authPromises);
|
|
|
|
for (let i = 0; i < authResults.length; i += 1) {
|
|
/**
|
|
* User has been successfully authenticated
|
|
*/
|
|
if (authResults[i]) return;
|
|
}
|
|
|
|
/**
|
|
* Unable to authenticate using any guard
|
|
*/
|
|
throw new AuthenticationException(
|
|
'Unauthorized access',
|
|
'E_UNAUTHORIZED_ACCESS',
|
|
guardLastAttempted,
|
|
this.redirectTo
|
|
);
|
|
}
|
|
|
|
public async handle(ctx: HttpContextContract, next: () => Promise<void>, customGuards: (keyof GuardsList)[]) {
|
|
/**
|
|
* Verify token from cookie and set the authorization header
|
|
*/
|
|
const token = ctx.request.cookie('token');
|
|
if (token) {
|
|
ctx.request.headers().authorization = `Bearer ${token}`;
|
|
}
|
|
|
|
/**
|
|
* Uses the user defined guards or the default guard mentioned in
|
|
* the config file
|
|
*/
|
|
const guards = customGuards.length ? customGuards : [ctx.auth.name];
|
|
await this.authenticate(ctx.auth, guards);
|
|
await next();
|
|
}
|
|
}
|