feat: migrate admins to better auth roles
This commit is contained in:
@@ -4,18 +4,21 @@ const passwordMessage = 'Password must be at least 8 characters.';
|
||||
const optionalPassword = z.string().refine((value) => value.length === 0 || value.length >= 8, {
|
||||
message: passwordMessage
|
||||
});
|
||||
const roleSchema = z.enum(['admin', 'user'], 'Select a role.');
|
||||
|
||||
export const createAdminSchema = z.object({
|
||||
name: z.string().trim().min(1, 'Enter a name.'),
|
||||
email: z.email('Enter a valid email address.').transform((value) => value.toLowerCase()),
|
||||
password: z.string().min(8, passwordMessage)
|
||||
password: z.string().min(8, passwordMessage),
|
||||
role: roleSchema.default('admin')
|
||||
});
|
||||
|
||||
export const editAdminSchema = z.object({
|
||||
id: z.string().min(1, 'Admin id is required.'),
|
||||
name: z.string().trim().min(1, 'Enter a name.'),
|
||||
email: z.email('Enter a valid email address.').transform((value) => value.toLowerCase()),
|
||||
password: optionalPassword
|
||||
password: optionalPassword,
|
||||
role: roleSchema
|
||||
});
|
||||
|
||||
export const deleteAdminSchema = z.object({
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { betterAuth } from 'better-auth/minimal';
|
||||
import { drizzleAdapter } from 'better-auth/adapters/drizzle';
|
||||
import { admin } from 'better-auth/plugins';
|
||||
import { sveltekitCookies } from 'better-auth/svelte-kit';
|
||||
import { env } from '$env/dynamic/private';
|
||||
import { getRequestEvent } from '$app/server';
|
||||
@@ -14,6 +15,7 @@ export const auth = betterAuth({
|
||||
disableSignUp: true
|
||||
},
|
||||
plugins: [
|
||||
admin(),
|
||||
sveltekitCookies(getRequestEvent) // make sure this is the last plugin in the array
|
||||
]
|
||||
});
|
||||
|
||||
@@ -13,7 +13,11 @@ export const user = sqliteTable('user', {
|
||||
updatedAt: integer('updated_at', { mode: 'timestamp_ms' })
|
||||
.default(sql`(cast(unixepoch('subsecond') * 1000 as integer))`)
|
||||
.$onUpdate(() => /* @__PURE__ */ new Date())
|
||||
.notNull()
|
||||
.notNull(),
|
||||
role: text('role'),
|
||||
banned: integer('banned', { mode: 'boolean' }).default(false),
|
||||
banReason: text('ban_reason'),
|
||||
banExpires: integer('ban_expires', { mode: 'timestamp_ms' })
|
||||
});
|
||||
|
||||
export const session = sqliteTable(
|
||||
@@ -32,7 +36,8 @@ export const session = sqliteTable(
|
||||
userAgent: text('user_agent'),
|
||||
userId: text('user_id')
|
||||
.notNull()
|
||||
.references(() => user.id, { onDelete: 'cascade' })
|
||||
.references(() => user.id, { onDelete: 'cascade' }),
|
||||
impersonatedBy: text('impersonated_by')
|
||||
},
|
||||
(table) => [index('session_userId_idx').on(table.userId)]
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user