mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-15 20:17:22 +00:00
refactor otp listeners
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import type { MaybePromise } from "bknd";
|
||||||
import type { Event } from "./Event";
|
import type { Event } from "./Event";
|
||||||
import type { EventClass } from "./EventManager";
|
import type { EventClass } from "./EventManager";
|
||||||
|
|
||||||
@@ -7,7 +8,7 @@ export type ListenerMode = (typeof ListenerModes)[number];
|
|||||||
export type ListenerHandler<E extends Event<any, any>> = (
|
export type ListenerHandler<E extends Event<any, any>> = (
|
||||||
event: E,
|
event: E,
|
||||||
slug: string,
|
slug: string,
|
||||||
) => E extends Event<any, infer R> ? R | Promise<R | void> : never;
|
) => E extends Event<any, infer R> ? MaybePromise<R | void> : never;
|
||||||
|
|
||||||
export class EventListener<E extends Event = Event> {
|
export class EventListener<E extends Event = Event> {
|
||||||
mode: ListenerMode = "async";
|
mode: ListenerMode = "async";
|
||||||
|
|||||||
@@ -256,4 +256,6 @@ describe("otp plugin", () => {
|
|||||||
// @todo: test invalid codes
|
// @todo: test invalid codes
|
||||||
// @todo: test codes with different actions
|
// @todo: test codes with different actions
|
||||||
// @todo: test code expiration
|
// @todo: test code expiration
|
||||||
|
// @todo: test code reuse
|
||||||
|
// @todo: test invalidation of previous codes when sending new code
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -103,7 +103,7 @@ export function emailOTP({
|
|||||||
}: EmailOTPPluginOptions = {}): AppPlugin {
|
}: EmailOTPPluginOptions = {}): AppPlugin {
|
||||||
return (app: App) => {
|
return (app: App) => {
|
||||||
return {
|
return {
|
||||||
name: "bknd-email-otp",
|
name: "email-otp",
|
||||||
schema: () =>
|
schema: () =>
|
||||||
em(
|
em(
|
||||||
{
|
{
|
||||||
@@ -348,20 +348,18 @@ async function invalidateAllUserCodes(app: App, entityName: string, email: strin
|
|||||||
}
|
}
|
||||||
|
|
||||||
function registerListeners(app: App, entityName: string) {
|
function registerListeners(app: App, entityName: string) {
|
||||||
app.emgr.onAny(
|
[DatabaseEvents.MutatorInsertBefore, DatabaseEvents.MutatorUpdateBefore].forEach((event) => {
|
||||||
(event) => {
|
app.emgr.onEvent(
|
||||||
if (
|
event,
|
||||||
event instanceof DatabaseEvents.MutatorInsertBefore ||
|
(e: { params: { entity: { name: string } } }) => {
|
||||||
event instanceof DatabaseEvents.MutatorUpdateBefore
|
if (e.params.entity.name === entityName) {
|
||||||
) {
|
|
||||||
if (event.params.entity.name === entityName) {
|
|
||||||
throw new OTPError("Mutations of the OTP entity are not allowed");
|
throw new OTPError("Mutations of the OTP entity are not allowed");
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
},
|
{
|
||||||
{
|
mode: "sync",
|
||||||
mode: "sync",
|
id: "bknd-email-otp",
|
||||||
id: "bknd-email-otp",
|
},
|
||||||
},
|
);
|
||||||
);
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user