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(
|
2024-08-11 10:42:42 +02:00
|
|
|
context: HttpContext,
|
2024-08-09 23:51:41 +02:00
|
|
|
next: NextFn,
|
|
|
|
options: {
|
|
|
|
guards?: Array<keyof Authenticators>
|
|
|
|
} = {},
|
|
|
|
): Promise<void> {
|
2024-08-11 10:42:42 +02:00
|
|
|
await context.auth.authenticateUsing(options.guards, {
|
2024-08-09 23:51:41 +02:00
|
|
|
loginRoute: this.redirectTo,
|
|
|
|
})
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
}
|