This repository has been archived on 2024-10-12. You can view files and clone it, but cannot push or open issues or pull requests.
wikipedia-game-solver/apps/api/src/app/middleware/auth_middleware.ts

28 lines
683 B
TypeScript
Raw Normal View History

2024-08-09 23:51:41 +02:00
import type { Authenticators } from "@adonisjs/auth/types"
import type { HttpContext } from "@adonisjs/core/http"
import type { NextFn } from "@adonisjs/core/types/http"
/**
* Auth middleware is used authenticate HTTP requests and deny
* access to unauthenticated users.
*/
export default class AuthMiddleware {
/**
* The URL to redirect to, when authentication fails
*/
redirectTo = "/login"
public async handle(
ctx: HttpContext,
next: NextFn,
options: {
guards?: Array<keyof Authenticators>
} = {},
): Promise<void> {
await ctx.auth.authenticateUsing(options.guards, {
loginRoute: this.redirectTo,
})
return next()
}
}