mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-17 04:46:05 +00:00
Merge remote-tracking branch 'origin/release/0.7' into fix/auth-api-include-cookie
# Conflicts: # app/src/auth/authenticate/Authenticator.ts
This commit is contained in:
@@ -21,6 +21,15 @@ export class AuthController extends Controller {
|
||||
return this.auth.ctx.guard;
|
||||
}
|
||||
|
||||
get em() {
|
||||
return this.auth.ctx.em;
|
||||
}
|
||||
|
||||
get userRepo() {
|
||||
const entity_name = this.auth.config.entity_name;
|
||||
return this.em.repo(entity_name as "users");
|
||||
}
|
||||
|
||||
private registerStrategyActions(strategy: Strategy, mainHono: Hono<ServerEnv>) {
|
||||
const actions = strategy.getActions?.();
|
||||
if (!actions) {
|
||||
@@ -96,7 +105,10 @@ export class AuthController extends Controller {
|
||||
|
||||
hono.get("/me", auth(), async (c) => {
|
||||
if (this.auth.authenticator.isUserLoggedIn()) {
|
||||
return c.json({ user: this.auth.authenticator.getUser() });
|
||||
const claims = this.auth.authenticator.getUser()!;
|
||||
const { data: user } = await this.userRepo.findId(claims.id);
|
||||
|
||||
return c.json({ user });
|
||||
}
|
||||
|
||||
return c.json({ user: null }, 403);
|
||||
|
||||
@@ -299,8 +299,8 @@ export class Authenticator<Strategies extends Record<string, Strategy> = 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
|
||||
@@ -312,7 +312,7 @@ export class Authenticator<Strategies extends Record<string, Strategy> = Record<
|
||||
}
|
||||
|
||||
async respond(c: Context, data: AuthResponse | Error | any, redirect?: string) {
|
||||
const successUrl = this.getSuccessPath(c);
|
||||
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 });
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user