|
8 | 8 | */ |
9 | 9 | import router from "@adonisjs/core/services/router"; |
10 | 10 |
|
11 | | -import User from "#models/user"; |
12 | | - |
13 | | -router.get("/auth/keycloak/redirect", ({ ally }) => { |
14 | | - return ally.use("keycloak").redirect(); |
15 | | -}); |
16 | | - |
17 | | -router.get("/auth/keycloak/callback", async ({ ally, auth }) => { |
18 | | - const keycloak = ally.use("keycloak"); |
19 | | - /** |
20 | | - * User has denied access by canceling |
21 | | - * the login flow |
22 | | - */ |
23 | | - if (keycloak.accessDenied()) { |
24 | | - return "You have cancelled the login process"; |
25 | | - } |
26 | | - |
27 | | - /** |
28 | | - * OAuth state verification failed. This happens when the |
29 | | - * CSRF cookie gets expired. |
30 | | - */ |
31 | | - if (keycloak.stateMisMatch()) { |
32 | | - return "We are unable to verify the request. Please try again"; |
33 | | - } |
34 | | - |
35 | | - /** |
36 | | - * GitHub responded with some error |
37 | | - */ |
38 | | - if (keycloak.hasError()) { |
39 | | - return keycloak.getError(); |
40 | | - } |
41 | | - |
42 | | - /** |
43 | | - * Access user info |
44 | | - */ |
45 | | - const keycloakUser = await keycloak.user(); |
46 | | - |
47 | | - const user = await User.firstOrCreate( |
48 | | - { email: keycloakUser.email }, |
49 | | - { |
50 | | - email: keycloakUser.email, |
51 | | - }, |
52 | | - ); |
53 | | - |
54 | | - auth.use("web").login(); |
55 | | - return user; |
56 | | -}); |
57 | | - |
58 | | -router.get("/", async () => { |
59 | | - return { |
60 | | - hello: "world", |
61 | | - }; |
62 | | -}); |
| 11 | +import { middleware } from "./kernel.js"; |
| 12 | + |
| 13 | +const AuthController = () => import("#controllers/auth_controller"); |
| 14 | + |
| 15 | +router.get("/auth/login", [AuthController, "login"]).use(middleware.guest()); |
| 16 | +router |
| 17 | + .get("/auth/callback", [AuthController, "callback"]) |
| 18 | + .use(middleware.guest()); |
| 19 | +router.post("/auth/logout", [AuthController, "logout"]).use(middleware.auth()); |
| 20 | + |
| 21 | +router |
| 22 | + .get("/", async ({ auth }) => { |
| 23 | + return { |
| 24 | + hello: "world", |
| 25 | + user: auth.user, |
| 26 | + }; |
| 27 | + }) |
| 28 | + .use(middleware.silentAuth()); |
0 commit comments