mirror of
https://github.com/jakejarvis/sofa.git
synced 2026-08-29 01:35:39 -04:00
First user to register automatically becomes admin and registration closes. Admins can re-open registration from a new settings page. Uses Better Auth admin plugin for role management and a new appSettings table for the registration flag. Mobile tab bar now links to settings instead of inline logout. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
57 lines
1.4 KiB
TypeScript
57 lines
1.4 KiB
TypeScript
import { betterAuth } from "better-auth";
|
|
import { drizzleAdapter } from "better-auth/adapters/drizzle";
|
|
import { APIError } from "better-auth/api";
|
|
import { admin } from "better-auth/plugins";
|
|
import { v4 as uuid } from "uuid";
|
|
import { db } from "@/lib/db/client";
|
|
import {
|
|
getUserCount,
|
|
isRegistrationOpen,
|
|
setSetting,
|
|
} from "@/lib/services/settings";
|
|
|
|
export const auth = betterAuth({
|
|
database: drizzleAdapter(db, {
|
|
provider: "sqlite",
|
|
}),
|
|
emailAndPassword: {
|
|
enabled: true,
|
|
},
|
|
plugins: [admin()],
|
|
advanced: {
|
|
database: {
|
|
generateId: () => uuid(),
|
|
},
|
|
},
|
|
databaseHooks: {
|
|
user: {
|
|
create: {
|
|
before: async (userData) => {
|
|
const userCount = await getUserCount();
|
|
if (userCount === 0) {
|
|
return {
|
|
data: {
|
|
...userData,
|
|
role: "admin",
|
|
},
|
|
};
|
|
}
|
|
const open = await isRegistrationOpen();
|
|
if (!open) {
|
|
throw new APIError("FORBIDDEN", {
|
|
message: "Registration is currently closed",
|
|
});
|
|
}
|
|
return { data: userData };
|
|
},
|
|
after: async () => {
|
|
const userCount = await getUserCount();
|
|
if (userCount === 1) {
|
|
await setSetting("registrationOpen", "false");
|
|
}
|
|
},
|
|
},
|
|
},
|
|
},
|
|
});
|