diff --git a/app/src/core/events/EventListener.ts b/app/src/core/events/EventListener.ts index 76a067e..7e8bc75 100644 --- a/app/src/core/events/EventListener.ts +++ b/app/src/core/events/EventListener.ts @@ -1,3 +1,4 @@ +import type { MaybePromise } from "bknd"; import type { Event } from "./Event"; import type { EventClass } from "./EventManager"; @@ -7,7 +8,7 @@ export type ListenerMode = (typeof ListenerModes)[number]; export type ListenerHandler> = ( event: E, slug: string, -) => E extends Event ? R | Promise : never; +) => E extends Event ? MaybePromise : never; export class EventListener { mode: ListenerMode = "async"; diff --git a/app/src/plugins/auth/email-otp.plugin.spec.ts b/app/src/plugins/auth/email-otp.plugin.spec.ts index 94fdbae..d0e0f78 100644 --- a/app/src/plugins/auth/email-otp.plugin.spec.ts +++ b/app/src/plugins/auth/email-otp.plugin.spec.ts @@ -256,4 +256,6 @@ describe("otp plugin", () => { // @todo: test invalid codes // @todo: test codes with different actions // @todo: test code expiration + // @todo: test code reuse + // @todo: test invalidation of previous codes when sending new code }); diff --git a/app/src/plugins/auth/email-otp.plugin.ts b/app/src/plugins/auth/email-otp.plugin.ts index 50b975b..b54dc6d 100644 --- a/app/src/plugins/auth/email-otp.plugin.ts +++ b/app/src/plugins/auth/email-otp.plugin.ts @@ -103,7 +103,7 @@ export function emailOTP({ }: EmailOTPPluginOptions = {}): AppPlugin { return (app: App) => { return { - name: "bknd-email-otp", + name: "email-otp", schema: () => em( { @@ -348,20 +348,18 @@ async function invalidateAllUserCodes(app: App, entityName: string, email: strin } function registerListeners(app: App, entityName: string) { - app.emgr.onAny( - (event) => { - if ( - event instanceof DatabaseEvents.MutatorInsertBefore || - event instanceof DatabaseEvents.MutatorUpdateBefore - ) { - if (event.params.entity.name === entityName) { + [DatabaseEvents.MutatorInsertBefore, DatabaseEvents.MutatorUpdateBefore].forEach((event) => { + app.emgr.onEvent( + event, + (e: { params: { entity: { name: string } } }) => { + if (e.params.entity.name === entityName) { throw new OTPError("Mutations of the OTP entity are not allowed"); } - } - }, - { - mode: "sync", - id: "bknd-email-otp", - }, - ); + }, + { + mode: "sync", + id: "bknd-email-otp", + }, + ); + }); }