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/app/middleware/auth_middleware.ts

27 lines
674 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"
/**
2024-08-12 19:19:43 +02:00
* Auth middleware is used authenticate HTTP requests and deny access to unauthenticated users.
2024-08-09 23:51:41 +02:00
*/
export default class AuthMiddleware {
/**
2024-08-12 19:19:43 +02:00
* The URL to redirect to, when authentication fails.
2024-08-09 23:51:41 +02:00
*/
redirectTo = "/login"
public async handle(
context: HttpContext,
2024-08-09 23:51:41 +02:00
next: NextFn,
options: {
guards?: Array<keyof Authenticators>
} = {},
2024-08-16 02:50:11 +02:00
) {
await context.auth.authenticateUsing(options.guards, {
2024-08-09 23:51:41 +02:00
loginRoute: this.redirectTo,
})
return next()
}
}