Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | // import { config } from "@snapback/config"; // TODO(REPO-001): Update when config export is available
import { getPendingInvitationByEmail } from "@snapback/platform/db/queries";
import type { BetterAuthPlugin } from "better-auth";
import { APIError } from "better-auth/api";
import { createAuthMiddleware } from "better-auth/plugins";
// Temporary config until @snapback/config is fixed
const config = {
auth: {
enableSignup: process.env.ENABLE_SIGNUP !== "false",
},
};
export const invitationOnlyPlugin = () =>
({
id: "invitationOnlyPlugin",
hooks: {
before: [
{
matcher: (context) => context.path?.startsWith("/sign-up/email") ?? false,
handler: createAuthMiddleware(async (ctx) => {
if (config.auth.enableSignup) {
return;
}
const { email } = ctx.body;
// check if there is an invitation for the email
const hasInvitation = await getPendingInvitationByEmail(email);
if (!hasInvitation) {
throw new APIError("BAD_REQUEST", {
code: "INVALID_INVITATION",
message: "No invitation found for this email",
});
}
}),
},
],
},
$ERROR_CODES: {
INVALID_INVITATION: "No invitation found for this email",
},
}) satisfies BetterAuthPlugin;
|