mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
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:
@@ -137,8 +137,13 @@ describe("otp plugin", () => {
|
||||
body: JSON.stringify({ email: "test@test.com" }),
|
||||
});
|
||||
expect(res.status).toBe(201);
|
||||
expect(await res.json()).toEqual({ sent: true, action: "login" } as any);
|
||||
const data = (await res.json()) as any;
|
||||
expect(data.sent).toBe(true);
|
||||
expect(data.data.email).toBe("test@test.com");
|
||||
expect(data.data.action).toBe("login");
|
||||
expect(data.data.expires_at).toBeDefined();
|
||||
|
||||
{
|
||||
const { data } = await app.em.fork().repo("users_otp").findOne({ email: "test@test.com" });
|
||||
expect(data?.code).toBeDefined();
|
||||
expect(data?.code?.length).toBe(6);
|
||||
@@ -146,6 +151,7 @@ describe("otp plugin", () => {
|
||||
true,
|
||||
);
|
||||
expect(data?.email).toBe("test@test.com");
|
||||
}
|
||||
expect(called).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
@@ -245,7 +251,11 @@ describe("otp plugin", () => {
|
||||
},
|
||||
body: JSON.stringify({ email: "test@test.com" }),
|
||||
});
|
||||
expect(await res.json()).toEqual({ sent: true, action: "register" } as any);
|
||||
const data = (await res.json()) as any;
|
||||
expect(data.sent).toBe(true);
|
||||
expect(data.data.email).toBe("test@test.com");
|
||||
expect(data.data.action).toBe("register");
|
||||
expect(data.data.expires_at).toBeDefined();
|
||||
|
||||
{
|
||||
const res = await app.server.request("/api/auth/otp/register", {
|
||||
|
||||
@@ -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,
|
||||
);
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user