feat: add role selection to user creation and UI updates
All checks were successful
Staging Build / build (push) Successful in 2m16s
All checks were successful
Staging Build / build (push) Successful in 2m16s
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user