feat: add role selection to user creation and UI updates
All checks were successful
Staging Build / build (push) Successful in 2m16s

This commit is contained in:
DanielS
2026-06-24 01:58:01 +02:00
parent 934879e5cf
commit e1f47ddcd0
3 changed files with 63 additions and 5 deletions

View File

@@ -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<string, string> = {}
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
}