Skip to content

Commit

Permalink
add registration toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
RealFascinated committed Mar 7, 2025
1 parent e2fbed4 commit c874d86
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/lib/auth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { nextCookies } from "better-auth/next-js";
import { createAuthMiddleware } from "better-auth/plugins";
import { admin } from "better-auth/plugins/admin";
import { username } from "better-auth/plugins/username";
import { db } from "./db/drizzle";
Expand Down Expand Up @@ -52,5 +53,29 @@ export const auth = betterAuth({
},
},
},
hooks: {
before: createAuthMiddleware(async (ctx) => {
const allowRegistrations = env.NEXT_PUBLIC_ALLOW_REGISTRATIONS;

switch (ctx.path) {
case "/callback/:id": {
if (!allowRegistrations) {
throw ctx.error("BAD_REQUEST", {
message: "Registering is currently not allowed",
});
}
break;
}
case "/sign-up/email": {
if (!allowRegistrations) {
throw ctx.error("BAD_REQUEST", {
message: "Registering is currently not allowed",
});
}
break;
}
}
}),
},
});
export type Session = typeof auth.$Infer.Session;
3 changes: 3 additions & 0 deletions src/lib/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const env = createEnv({

shared: {
NEXT_PUBLIC_APP_ENV: z.string(),
NEXT_PUBLIC_ALLOW_REGISTRATIONS: z.boolean().optional().default(true),
},

server: {
Expand Down Expand Up @@ -45,6 +46,8 @@ export const env = createEnv({

// Shared
NEXT_PUBLIC_APP_ENV: process.env.NEXT_PUBLIC_APP_ENV ?? "development",
NEXT_PUBLIC_ALLOW_REGISTRATIONS:
process.env.NEXT_PUBLIC_ALLOW_REGISTRATIONS === "true",

// Postgres
DATABASE_URL: process.env.DATABASE_URL,
Expand Down

0 comments on commit c874d86

Please sign in to comment.