mirror of
https://github.com/bknd-io/bknd/
synced 2026-08-04 00:56:01 +00:00
refactor error handling in authenticator and password strategy (#161)
made `respondWithError` method public, updated login and register routes in `PasswordStrategy` to handle errors using `respondWithError` for consistency.
This commit is contained in:
@@ -215,7 +215,7 @@ export class Authenticator<Strategies extends Record<string, Strategy> = Record<
|
|||||||
throw new Exception("Invalid response");
|
throw new Exception("Invalid response");
|
||||||
}
|
}
|
||||||
|
|
||||||
private async respondWithError(c: Context, error: Error, opts?: AuthResolveOptions) {
|
async respondWithError(c: Context, error: Error, opts?: AuthResolveOptions) {
|
||||||
$console.error("respondWithError", error);
|
$console.error("respondWithError", error);
|
||||||
if (this.isJsonRequest(c) || opts?.forceJsonResponse) {
|
if (this.isJsonRequest(c) || opts?.forceJsonResponse) {
|
||||||
// let the server handle it
|
// let the server handle it
|
||||||
@@ -224,7 +224,7 @@ export class Authenticator<Strategies extends Record<string, Strategy> = Record<
|
|||||||
|
|
||||||
await addFlashMessage(c, String(error), "error");
|
await addFlashMessage(c, String(error), "error");
|
||||||
|
|
||||||
const referer = opts?.redirect ?? c.req.header("Referer") ?? "/";
|
const referer = this.getSafeUrl(c, opts?.redirect ?? c.req.header("Referer") ?? "/");
|
||||||
return c.redirect(referer);
|
return c.redirect(referer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -85,37 +85,49 @@ export class PasswordStrategy extends Strategy<typeof schema> {
|
|||||||
const payloadSchema = this.getPayloadSchema();
|
const payloadSchema = this.getPayloadSchema();
|
||||||
|
|
||||||
hono.post("/login", tb("query", redirectQuerySchema), async (c) => {
|
hono.post("/login", tb("query", redirectQuerySchema), async (c) => {
|
||||||
const body = parse(payloadSchema, await authenticator.getBody(c), {
|
try {
|
||||||
onError: (errors) => {
|
const body = parse(payloadSchema, await authenticator.getBody(c), {
|
||||||
$console.error("Invalid login payload", [...errors]);
|
onError: (errors) => {
|
||||||
throw new InvalidCredentialsException();
|
$console.error("Invalid login payload", [...errors]);
|
||||||
},
|
throw new InvalidCredentialsException();
|
||||||
});
|
},
|
||||||
const { redirect } = c.req.valid("query");
|
});
|
||||||
|
const { redirect } = c.req.valid("query");
|
||||||
|
|
||||||
return await authenticator.resolveLogin(c, this, body, this.verify(body.password), {
|
return await authenticator.resolveLogin(c, this, body, this.verify(body.password), {
|
||||||
redirect,
|
redirect,
|
||||||
});
|
});
|
||||||
|
} catch (e) {
|
||||||
|
return authenticator.respondWithError(c, e as any);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
hono.post("/register", tb("query", redirectQuerySchema), async (c) => {
|
hono.post("/register", tb("query", redirectQuerySchema), async (c) => {
|
||||||
const { redirect } = c.req.valid("query");
|
try {
|
||||||
const { password, email, ...body } = parse(payloadSchema, await authenticator.getBody(c), {
|
const { redirect } = c.req.valid("query");
|
||||||
onError: (errors) => {
|
const { password, email, ...body } = parse(
|
||||||
$console.error("Invalid register payload", [...errors]);
|
payloadSchema,
|
||||||
throw new InvalidCredentialsException();
|
await authenticator.getBody(c),
|
||||||
},
|
{
|
||||||
});
|
onError: (errors) => {
|
||||||
|
$console.error("Invalid register payload", [...errors]);
|
||||||
|
new InvalidCredentialsException();
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
const profile = {
|
const profile = {
|
||||||
...body,
|
...body,
|
||||||
email,
|
email,
|
||||||
strategy_value: await this.hash(password),
|
strategy_value: await this.hash(password),
|
||||||
};
|
};
|
||||||
|
|
||||||
return await authenticator.resolveRegister(c, this, profile, async () => void 0, {
|
return await authenticator.resolveRegister(c, this, profile, async () => void 0, {
|
||||||
redirect,
|
redirect,
|
||||||
});
|
});
|
||||||
|
} catch (e) {
|
||||||
|
return authenticator.respondWithError(c, e as any);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return hono;
|
return hono;
|
||||||
|
|||||||
Reference in New Issue
Block a user