Return data in my plugin
Hello, I would like my plugin to add additional information about the email to the request data, including types, and for it to be accessible on the server side from the request, but I don't know how to do it. Does anyone know?
export const dymoEmailPlugin = ({ apiKey, emailRules }: DymoEmailPluginOptions) => {
const defaultRules: EmailValidatorRules = {
deny: ["FRAUD", "INVALID", "NO_MX_RECORDS", "NO_REPLY_EMAIL"] as NegativeEmailRules[]
};
const dymoClient = new DymoAPI({
apiKey,
rules: {
email: {
deny: emailRules?.deny ?? defaultRules.deny
}
}
});
return {
id: "dymoEmailPlugin",
hooks: {
before: [
{
matcher: (context: any) => context.path.startsWith("/sign-up/email"),
handler: createAuthMiddleware(async (ctx: any) => {
const { email } = ctx.body;
if (typeof email !== "string") throw new APIError("BAD_REQUEST", { message: "Email must be a string." });
const decision = await dymoClient.isValidEmail(email);
if (!decision.allow) {
throw new APIError("BAD_REQUEST", {
message: "Email is invalid or blocked.",
reasons: decision.reasons
});
}
ctx.dymoEmail = decision.response as DataEmailValidationAnalysis;
return { context: ctx };
})
}
]
}
} satisfies BetterAuthPlugin;
};export const dymoEmailPlugin = ({ apiKey, emailRules }: DymoEmailPluginOptions) => {
const defaultRules: EmailValidatorRules = {
deny: ["FRAUD", "INVALID", "NO_MX_RECORDS", "NO_REPLY_EMAIL"] as NegativeEmailRules[]
};
const dymoClient = new DymoAPI({
apiKey,
rules: {
email: {
deny: emailRules?.deny ?? defaultRules.deny
}
}
});
return {
id: "dymoEmailPlugin",
hooks: {
before: [
{
matcher: (context: any) => context.path.startsWith("/sign-up/email"),
handler: createAuthMiddleware(async (ctx: any) => {
const { email } = ctx.body;
if (typeof email !== "string") throw new APIError("BAD_REQUEST", { message: "Email must be a string." });
const decision = await dymoClient.isValidEmail(email);
if (!decision.allow) {
throw new APIError("BAD_REQUEST", {
message: "Email is invalid or blocked.",
reasons: decision.reasons
});
}
ctx.dymoEmail = decision.response as DataEmailValidationAnalysis;
return { context: ctx };
})
}
]
}
} satisfies BetterAuthPlugin;
};