mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
added a redirect param to password strategy
This commit is contained in:
@@ -299,8 +299,8 @@ export class Authenticator<Strategies extends Record<string, Strategy> = Record<
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private getSuccessPath(c: Context) {
|
private getSafeUrl(c: Context, path: string) {
|
||||||
const p = (this.config.cookie.pathSuccess ?? "/").replace(/\/+$/, "/");
|
const p = path.replace(/\/+$/, "/");
|
||||||
|
|
||||||
// nextjs doesn't support non-fq urls
|
// nextjs doesn't support non-fq urls
|
||||||
// but env could be proxied (stackblitz), so we shouldn't fq every url
|
// but env could be proxied (stackblitz), so we shouldn't fq every url
|
||||||
@@ -316,7 +316,10 @@ export class Authenticator<Strategies extends Record<string, Strategy> = Record<
|
|||||||
return c.json(data);
|
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;
|
const referer = redirect ?? c.req.header("Referer") ?? successUrl;
|
||||||
//console.log("auth respond", { redirect, successUrl, successPath });
|
//console.log("auth respond", { redirect, successUrl, successPath });
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import type { Authenticator, Strategy } from "auth";
|
import type { Authenticator, Strategy } from "auth";
|
||||||
|
import { isDebug, tbValidator as tb } from "core";
|
||||||
import { type Static, StringEnum, Type, parse } from "core/utils";
|
import { type Static, StringEnum, Type, parse } from "core/utils";
|
||||||
import { hash } from "core/utils";
|
import { hash } from "core/utils";
|
||||||
import { type Context, Hono } from "hono";
|
import { type Context, Hono } from "hono";
|
||||||
@@ -56,26 +57,56 @@ export class PasswordStrategy implements Strategy {
|
|||||||
const hono = new Hono();
|
const hono = new Hono();
|
||||||
|
|
||||||
return hono
|
return hono
|
||||||
.post("/login", async (c) => {
|
.post(
|
||||||
const body = await authenticator.getBody(c);
|
"/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 {
|
try {
|
||||||
const payload = await this.login(body);
|
const payload = await this.login(body);
|
||||||
const data = await authenticator.resolve("login", this, payload.password, payload);
|
const data = await authenticator.resolve(
|
||||||
|
"login",
|
||||||
|
this,
|
||||||
|
payload.password,
|
||||||
|
payload
|
||||||
|
);
|
||||||
|
|
||||||
return await authenticator.respond(c, data);
|
return await authenticator.respond(c, data, redirect);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return await authenticator.respond(c, e);
|
return await authenticator.respond(c, e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
)
|
||||||
.post("/register", async (c) => {
|
.post(
|
||||||
const body = await authenticator.getBody(c);
|
"/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 payload = await this.register(body);
|
||||||
const data = await authenticator.resolve("register", this, payload.password, payload);
|
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 {
|
getActions(): StrategyActions {
|
||||||
|
|||||||
Reference in New Issue
Block a user