From 2b5e1771dec3a459bd5e3533d0182813bae8e6a2 Mon Sep 17 00:00:00 2001 From: dswbx Date: Fri, 14 Nov 2025 21:59:06 +0100 Subject: [PATCH] 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 --- app/src/plugins/auth/email-otp.plugin.spec.ts | 28 ++++++++++------ app/src/plugins/auth/email-otp.plugin.ts | 32 ++++++++++++++++--- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/app/src/plugins/auth/email-otp.plugin.spec.ts b/app/src/plugins/auth/email-otp.plugin.spec.ts index 84d1a47..cadc0a3 100644 --- a/app/src/plugins/auth/email-otp.plugin.spec.ts +++ b/app/src/plugins/auth/email-otp.plugin.spec.ts @@ -137,15 +137,21 @@ 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); - expect(data?.code?.split("").every((char: string) => Number.isInteger(Number(char)))).toBe( - true, - ); - expect(data?.email).toBe("test@test.com"); + { + const { data } = await app.em.fork().repo("users_otp").findOne({ email: "test@test.com" }); + expect(data?.code).toBeDefined(); + expect(data?.code?.length).toBe(6); + expect(data?.code?.split("").every((char: string) => Number.isInteger(Number(char)))).toBe( + 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", { diff --git a/app/src/plugins/auth/email-otp.plugin.ts b/app/src/plugins/auth/email-otp.plugin.ts index c75a1d7..13f9a93 100644 --- a/app/src/plugins/auth/email-otp.plugin.ts +++ b/app/src/plugins/auth/email-otp.plugin.ts @@ -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, + ); } }, )