|
1 | 1 | import type { Authenticator, Strategy } from "auth";
|
| 2 | +import { isDebug, tbValidator as tb } from "core"; |
2 | 3 | import { type Static, StringEnum, Type, parse } from "core/utils";
|
3 | 4 | import { hash } from "core/utils";
|
4 | 5 | import { type Context, Hono } from "hono";
|
@@ -56,26 +57,56 @@ export class PasswordStrategy implements Strategy {
|
56 | 57 | const hono = new Hono();
|
57 | 58 |
|
58 | 59 | return hono
|
59 |
| - .post("/login", async (c) => { |
60 |
| - const body = await authenticator.getBody(c); |
61 |
| - |
62 |
| - try { |
63 |
| - const payload = await this.login(body); |
64 |
| - const data = await authenticator.resolve("login", this, payload.password, payload); |
65 |
| - |
66 |
| - return await authenticator.respond(c, data); |
67 |
| - } catch (e) { |
68 |
| - return await authenticator.respond(c, e); |
| 60 | + .post( |
| 61 | + "/login", |
| 62 | + tb( |
| 63 | + "query", |
| 64 | + Type.Object({ |
| 65 | + redirect: Type.Optional(Type.String()) |
| 66 | + }) |
| 67 | + ), |
| 68 | + async (c) => { |
| 69 | + const body = await authenticator.getBody(c); |
| 70 | + const { redirect } = c.req.valid("query"); |
| 71 | + |
| 72 | + try { |
| 73 | + const payload = await this.login(body); |
| 74 | + const data = await authenticator.resolve( |
| 75 | + "login", |
| 76 | + this, |
| 77 | + payload.password, |
| 78 | + payload |
| 79 | + ); |
| 80 | + |
| 81 | + return await authenticator.respond(c, data, redirect); |
| 82 | + } catch (e) { |
| 83 | + return await authenticator.respond(c, e); |
| 84 | + } |
69 | 85 | }
|
70 |
| - }) |
71 |
| - .post("/register", async (c) => { |
72 |
| - const body = await authenticator.getBody(c); |
73 |
| - |
74 |
| - const payload = await this.register(body); |
75 |
| - const data = await authenticator.resolve("register", this, payload.password, payload); |
76 |
| - |
77 |
| - return await authenticator.respond(c, data); |
78 |
| - }); |
| 86 | + ) |
| 87 | + .post( |
| 88 | + "/register", |
| 89 | + tb( |
| 90 | + "query", |
| 91 | + Type.Object({ |
| 92 | + redirect: Type.Optional(Type.String()) |
| 93 | + }) |
| 94 | + ), |
| 95 | + async (c) => { |
| 96 | + const body = await authenticator.getBody(c); |
| 97 | + const { redirect } = c.req.valid("query"); |
| 98 | + |
| 99 | + const payload = await this.register(body); |
| 100 | + const data = await authenticator.resolve( |
| 101 | + "register", |
| 102 | + this, |
| 103 | + payload.password, |
| 104 | + payload |
| 105 | + ); |
| 106 | + |
| 107 | + return await authenticator.respond(c, data, redirect); |
| 108 | + } |
| 109 | + ); |
79 | 110 | }
|
80 | 111 |
|
81 | 112 | getActions(): StrategyActions {
|
|
0 commit comments