mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-15 20:17:22 +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,15 +137,21 @@ describe("otp plugin", () => {
|
|||||||
body: JSON.stringify({ email: "test@test.com" }),
|
body: JSON.stringify({ email: "test@test.com" }),
|
||||||
});
|
});
|
||||||
expect(res.status).toBe(201);
|
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();
|
const { data } = await app.em.fork().repo("users_otp").findOne({ email: "test@test.com" });
|
||||||
expect(data?.code?.length).toBe(6);
|
expect(data?.code).toBeDefined();
|
||||||
expect(data?.code?.split("").every((char: string) => Number.isInteger(Number(char)))).toBe(
|
expect(data?.code?.length).toBe(6);
|
||||||
true,
|
expect(data?.code?.split("").every((char: string) => Number.isInteger(Number(char)))).toBe(
|
||||||
);
|
true,
|
||||||
expect(data?.email).toBe("test@test.com");
|
);
|
||||||
|
expect(data?.email).toBe("test@test.com");
|
||||||
|
}
|
||||||
expect(called).toHaveBeenCalled();
|
expect(called).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -245,7 +251,11 @@ describe("otp plugin", () => {
|
|||||||
},
|
},
|
||||||
body: JSON.stringify({ email: "test@test.com" }),
|
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", {
|
const res = await app.server.request("/api/auth/otp/register", {
|
||||||
|
|||||||
@@ -13,7 +13,16 @@ import {
|
|||||||
type EntityConfig,
|
type EntityConfig,
|
||||||
DatabaseEvents,
|
DatabaseEvents,
|
||||||
} from "bknd";
|
} 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";
|
import { Hono } from "hono";
|
||||||
|
|
||||||
export type EmailOTPPluginOptions = {
|
export type EmailOTPPluginOptions = {
|
||||||
@@ -110,10 +119,11 @@ export function emailOTP({
|
|||||||
[entityName]: entity(
|
[entityName]: entity(
|
||||||
entityName,
|
entityName,
|
||||||
otpFields,
|
otpFields,
|
||||||
entityConfig ?? {
|
{
|
||||||
name: "Users OTP",
|
name: "Users OTP",
|
||||||
sort_dir: "desc",
|
sort_dir: "desc",
|
||||||
primary_format: app.module.data.config.default_primary_format,
|
primary_format: app.module.data.config.default_primary_format,
|
||||||
|
...entityConfig,
|
||||||
},
|
},
|
||||||
"generated",
|
"generated",
|
||||||
),
|
),
|
||||||
@@ -182,7 +192,13 @@ export function emailOTP({
|
|||||||
await sendCode(app, otpData, { generateEmail });
|
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({
|
const user = await app.createUser({
|
||||||
email,
|
email,
|
||||||
password: randomString(16, true),
|
password: randomString(32, true),
|
||||||
});
|
});
|
||||||
|
|
||||||
const jwt = await auth.authenticator.jwt(user);
|
const jwt = await auth.authenticator.jwt(user);
|
||||||
@@ -238,7 +254,13 @@ export function emailOTP({
|
|||||||
await sendCode(app, otpData, { generateEmail });
|
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