From 10db26716451832c6a01d894715f6b1d3b465cc5 Mon Sep 17 00:00:00 2001 From: dswbx Date: Wed, 29 Jan 2025 20:26:06 +0100 Subject: [PATCH 1/2] added a `redirect` param to password strategy --- app/src/auth/authenticate/Authenticator.ts | 9 ++- .../strategies/PasswordStrategy.ts | 61 ++++++++++++++----- 2 files changed, 52 insertions(+), 18 deletions(-) diff --git a/app/src/auth/authenticate/Authenticator.ts b/app/src/auth/authenticate/Authenticator.ts index 19088d9..63f4f22 100644 --- a/app/src/auth/authenticate/Authenticator.ts +++ b/app/src/auth/authenticate/Authenticator.ts @@ -299,8 +299,8 @@ export class Authenticator = Record< } } - private getSuccessPath(c: Context) { - const p = (this.config.cookie.pathSuccess ?? "/").replace(/\/+$/, "/"); + private getSafeUrl(c: Context, path: string) { + const p = path.replace(/\/+$/, "/"); // nextjs doesn't support non-fq urls // but env could be proxied (stackblitz), so we shouldn't fq every url @@ -316,7 +316,10 @@ export class Authenticator = Record< return c.json(data); } - const successUrl = this.getSuccessPath(c); + const successUrl = this.getSafeUrl( + c, + redirect ? redirect : (this.config.cookie.pathSuccess ?? "/") + ); const referer = redirect ?? c.req.header("Referer") ?? successUrl; //console.log("auth respond", { redirect, successUrl, successPath }); diff --git a/app/src/auth/authenticate/strategies/PasswordStrategy.ts b/app/src/auth/authenticate/strategies/PasswordStrategy.ts index d8f8a23..c6a9a37 100644 --- a/app/src/auth/authenticate/strategies/PasswordStrategy.ts +++ b/app/src/auth/authenticate/strategies/PasswordStrategy.ts @@ -1,4 +1,5 @@ import type { Authenticator, Strategy } from "auth"; +import { isDebug, tbValidator as tb } from "core"; import { type Static, StringEnum, Type, parse } from "core/utils"; import { hash } from "core/utils"; import { type Context, Hono } from "hono"; @@ -56,26 +57,56 @@ export class PasswordStrategy implements Strategy { const hono = new Hono(); return hono - .post("/login", async (c) => { - const body = await authenticator.getBody(c); + .post( + "/login", + tb( + "query", + Type.Object({ + redirect: Type.Optional(Type.String()) + }) + ), + async (c) => { + const body = await authenticator.getBody(c); + const { redirect } = c.req.valid("query"); - try { - const payload = await this.login(body); - const data = await authenticator.resolve("login", this, payload.password, payload); + try { + const payload = await this.login(body); + const data = await authenticator.resolve( + "login", + this, + payload.password, + payload + ); - return await authenticator.respond(c, data); - } catch (e) { - return await authenticator.respond(c, e); + return await authenticator.respond(c, data, redirect); + } catch (e) { + return await authenticator.respond(c, e); + } } - }) - .post("/register", async (c) => { - const body = await authenticator.getBody(c); + ) + .post( + "/register", + tb( + "query", + Type.Object({ + redirect: Type.Optional(Type.String()) + }) + ), + async (c) => { + const body = await authenticator.getBody(c); + const { redirect } = c.req.valid("query"); - const payload = await this.register(body); - const data = await authenticator.resolve("register", this, payload.password, payload); + const payload = await this.register(body); + const data = await authenticator.resolve( + "register", + this, + payload.password, + payload + ); - return await authenticator.respond(c, data); - }); + return await authenticator.respond(c, data, redirect); + } + ); } getActions(): StrategyActions { From 4755288fedd93a12825d5c9bf0157014a4784177 Mon Sep 17 00:00:00 2001 From: dswbx Date: Wed, 29 Jan 2025 20:30:27 +0100 Subject: [PATCH 2/2] improve fallbacks for redirection --- app/src/auth/authenticate/Authenticator.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/app/src/auth/authenticate/Authenticator.ts b/app/src/auth/authenticate/Authenticator.ts index 63f4f22..13fa6bc 100644 --- a/app/src/auth/authenticate/Authenticator.ts +++ b/app/src/auth/authenticate/Authenticator.ts @@ -316,10 +316,7 @@ export class Authenticator = Record< return c.json(data); } - const successUrl = this.getSafeUrl( - c, - redirect ? redirect : (this.config.cookie.pathSuccess ?? "/") - ); + const successUrl = this.getSafeUrl(c, redirect ?? this.config.cookie.pathSuccess ?? "/"); const referer = redirect ?? c.req.header("Referer") ?? successUrl; //console.log("auth respond", { redirect, successUrl, successPath });