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:
@@ -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<typeof userSchema>
|
||||
@@ -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() {
|
||||
<DialogHeader>
|
||||
<DialogTitle>Neuen Benutzer anlegen</DialogTitle>
|
||||
<DialogDescription className="text-slate-300">
|
||||
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.
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
@@ -87,6 +95,25 @@ export function UserDialog() {
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="role"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel className="text-white">Rolle</FormLabel>
|
||||
<FormControl>
|
||||
<select
|
||||
className="flex h-10 w-full rounded-md border border-white/10 bg-slate-950/80 px-3 py-2 text-sm text-white focus:outline-none focus:ring-2 focus:ring-primary focus:ring-offset-2"
|
||||
{...field}
|
||||
>
|
||||
<option value="partner">Partner</option>
|
||||
<option value="admin">Admin</option>
|
||||
</select>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="password"
|
||||
|
||||
@@ -53,6 +53,7 @@ export function UserList({ initialUsers }: { initialUsers: any[] }) {
|
||||
<TableHeader>
|
||||
<TableRow className="border-white/10 hover:bg-white/5">
|
||||
<TableHead className="text-slate-200">E-Mail</TableHead>
|
||||
<TableHead className="text-slate-200">Rolle</TableHead>
|
||||
<TableHead className="text-slate-200">Status</TableHead>
|
||||
<TableHead className="text-slate-200">Zuletzt angemeldet</TableHead>
|
||||
<TableHead className="text-right text-slate-200">Aktionen</TableHead>
|
||||
@@ -69,6 +70,11 @@ export function UserList({ initialUsers }: { initialUsers: any[] }) {
|
||||
{user.email}
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Badge className={user.role === 'admin' ? "bg-purple-500/20 text-purple-400 border border-purple-500/30" : "bg-blue-500/20 text-blue-400 border border-blue-500/30"}>
|
||||
{user.role === 'admin' ? 'Admin' : 'Partner'}
|
||||
</Badge>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Badge variant={isBanned ? "destructive" : "default"} className={isBanned ? "" : "bg-green-500/20 text-green-500 border-green-500/30"}>
|
||||
{isBanned ? "Gesperrt" : "Aktiv"}
|
||||
|
||||
@@ -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
|
||||
})
|
||||
}
|
||||
|
||||
export async function createUser(data: { email: string; password?: string; email_confirm?: boolean }) {
|
||||
return users.map(u => ({
|
||||
...u,
|
||||
role: roleMap[u.id] || 'partner'
|
||||
}))
|
||||
}
|
||||
|
||||
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