From e1f47ddcd0e110cf0430fd2b02929f5f2f4f1799 Mon Sep 17 00:00:00 2001 From: DanielS Date: Wed, 24 Jun 2026 01:58:01 +0200 Subject: [PATCH] feat: add role selection to user creation and UI updates --- shop/components/admin/user-dialog.tsx | 33 ++++++++++++++++++++++++--- shop/components/admin/user-list.tsx | 6 +++++ shop/lib/actions/users.ts | 29 +++++++++++++++++++++-- 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/shop/components/admin/user-dialog.tsx b/shop/components/admin/user-dialog.tsx index 70bb722..36053b8 100644 --- a/shop/components/admin/user-dialog.tsx +++ b/shop/components/admin/user-dialog.tsx @@ -29,7 +29,8 @@ import { Plus } from 'lucide-react' const userSchema = z.object({ email: z.string().email('Ungültige E-Mail-Adresse'), - password: z.string().min(6, 'Passwort muss mindestens 6 Zeichen lang sein').optional(), + password: z.string().min(6, 'Passwort muss mindestens 6 Zeichen lang sein').optional().or(z.literal('')), + role: z.enum(['admin', 'partner']), }) type UserFormValues = z.infer @@ -43,12 +44,19 @@ export function UserDialog() { defaultValues: { email: '', password: '', + role: 'partner', }, }) async function onSubmit(values: UserFormValues) { try { - await createUser(values) + // If password is empty string, omit it + const payload = { + email: values.email, + role: values.role, + ...(values.password ? { password: values.password } : {}) + } + await createUser(payload) setOpen(false) form.reset() router.refresh() @@ -68,7 +76,7 @@ export function UserDialog() { Neuen Benutzer anlegen - Geben Sie die E-Mail und optional ein Passwort für den neuen Benutzer ein. + Geben Sie die E-Mail, die Rolle und optional ein Passwort für den neuen Benutzer ein. @@ -87,6 +95,25 @@ export function UserDialog() { )} /> + ( + + Rolle + + + + + + )} + /> E-Mail + Rolle Status Zuletzt angemeldet Aktionen @@ -69,6 +70,11 @@ export function UserList({ initialUsers }: { initialUsers: any[] }) { {user.email} + + + {user.role === 'admin' ? 'Admin' : 'Partner'} + + {isBanned ? "Gesperrt" : "Aktiv"} diff --git a/shop/lib/actions/users.ts b/shop/lib/actions/users.ts index b325a82..55e51c1 100644 --- a/shop/lib/actions/users.ts +++ b/shop/lib/actions/users.ts @@ -7,10 +7,25 @@ export async function getUsers() { const admin = createAdminClient() const { data: { users }, error } = await admin.auth.admin.listUsers() if (error) throw error - return users + + const { data: dbUsers, error: dbError } = await admin + .from('users') + .select('id, role') + + const roleMap: Record = {} + if (!dbError && dbUsers) { + dbUsers.forEach(u => { + roleMap[u.id] = u.role + }) + } + + return users.map(u => ({ + ...u, + role: roleMap[u.id] || 'partner' + })) } -export async function createUser(data: { email: string; password?: string; email_confirm?: boolean }) { +export async function createUser(data: { email: string; password?: string; role?: 'admin' | 'partner'; email_confirm?: boolean }) { const admin = createAdminClient() const { data: { user }, error } = await admin.auth.admin.createUser({ email: data.email, @@ -18,6 +33,16 @@ export async function createUser(data: { email: string; password?: string; email email_confirm: data.email_confirm ?? true, }) if (error) throw error + if (!user) throw new Error('Benutzer konnte nicht erstellt werden.') + + // Upsert user role in public.users table + const role = data.role || 'partner' + const { error: dbError } = await admin + .from('users') + .upsert({ id: user.id, role }, { onConflict: 'id' }) + + if (dbError) throw dbError + revalidatePath('/admin/users') return user }