refactor: enhance email OTP responses and improve data structure

Updated the email OTP plugin to return a structured response containing the email, action, and expiration details. Adjusted the tests to validate the new response format. Increased password length for user creation
This commit is contained in:
dswbx
2025-11-14 21:59:06 +01:00
parent a16e017e39
commit 2b5e1771de
2 changed files with 46 additions and 14 deletions
+27 -5
View File
@@ -13,7 +13,16 @@ import {
type EntityConfig,
DatabaseEvents,
} from "bknd";
import { invariant, s, jsc, HttpStatus, threwAsync, randomString, $console } from "bknd/utils";
import {
invariant,
s,
jsc,
HttpStatus,
threwAsync,
randomString,
$console,
pickKeys,
} from "bknd/utils";
import { Hono } from "hono";
export type EmailOTPPluginOptions = {
@@ -110,10 +119,11 @@ export function emailOTP({
[entityName]: entity(
entityName,
otpFields,
entityConfig ?? {
{
name: "Users OTP",
sort_dir: "desc",
primary_format: app.module.data.config.default_primary_format,
...entityConfig,
},
"generated",
),
@@ -182,7 +192,13 @@ export function emailOTP({
await sendCode(app, otpData, { generateEmail });
}
return c.json({ sent: true, action: "login" }, HttpStatus.CREATED);
return c.json(
{
sent: true,
data: pickKeys(otpData, ["email", "action", "expires_at"]),
},
HttpStatus.CREATED,
);
}
},
)
@@ -217,7 +233,7 @@ export function emailOTP({
const user = await app.createUser({
email,
password: randomString(16, true),
password: randomString(32, true),
});
const jwt = await auth.authenticator.jwt(user);
@@ -238,7 +254,13 @@ export function emailOTP({
await sendCode(app, otpData, { generateEmail });
}
return c.json({ sent: true, action: "register" }, HttpStatus.CREATED);
return c.json(
{
sent: true,
data: pickKeys(otpData, ["email", "action", "expires_at"]),
},
HttpStatus.CREATED,
);
}
},
)