fix admin edit
All checks were successful
deploy / build-and-deploy (push) Successful in 22s

This commit is contained in:
bytedream 2025-05-18 17:53:06 +02:00
parent 078ae89708
commit 7ac248baf5
6 changed files with 21 additions and 9 deletions

View File

@ -24,7 +24,7 @@ export const admin = {
input: z.object({
id: z.number(),
username: z.string(),
password: z.string(),
password: z.string().nullable(),
permissions: z.number()
}),
handler: async (input, context) => {

View File

@ -32,7 +32,7 @@
</tr>
</thead>
<tbody>
{#each $admins as admin, i (admin.id)}
{#each $admins as admin, i (admin)}
<tr class="hover:bg-base-200">
<td>{i + 1}</td>
<td>{admin.username}</td>

View File

@ -23,6 +23,7 @@
const index = reactiveSet.indexOf(badge);
if (index !== -1) {
reactiveSet.splice(index, 1);
onUpdate?.(reactiveSet);
}
}
</script>

View File

@ -44,7 +44,7 @@
let password = $state<string | null>(null);
let permissions = $state<number | null>(admin?.permissions ?? 0);
let submitEnabled = $derived(!!(username && password));
let submitEnabled = $derived(!!username && (admin || password));
// lifecycle
$effect(() => {
@ -85,7 +85,7 @@
<div class="w-full gap-x-4 gap-y-2">
<div class="w-[20rem]">
<Input type="text" bind:value={username} label="Username" required />
<Password bind:value={password} label="Password" required />
<Password bind:value={password} label="Password" required={!admin} />
</div>
<fieldset class="fieldset">
<legend class="fieldset-legend">Berechtigungen</legend>

View File

@ -15,7 +15,12 @@ export const admin = mysqlTable('admin', {
export type AddAdminReq = Omit<typeof admin.$inferInsert, 'id'>;
export type EditAdminReq = Omit<typeof admin.$inferInsert, 'id'> & { id: number };
export type EditAdminReq = {
id: number;
username: string;
password: string | null;
permissions: number;
};
export type GetAdminReq = {};
export type GetAdminRes = Omit<typeof admin.$inferSelect, 'password'>[];
@ -39,9 +44,15 @@ export async function addAdmin(db: Database, values: AddAdminReq) {
}
export async function editAdmin(db: Database, values: EditAdminReq) {
values.password = bcrypt.hashSync(values.password, 10);
await db.update(admin).set(values).where(eq(admin.id, values.id));
return db
.update(admin)
.set({
id: values.id,
username: values.username,
password: values.password != null ? bcrypt.hashSync(values.password, 10) : undefined,
permissions: values.permissions
})
.where(eq(admin.id, values.id));
}
export async function getAdmins(db: Database, _values: GetAdminReq): Promise<GetAdminRes> {

View File

@ -17,7 +17,7 @@ const session = Session.sessionFromCookies(Astro.cookies);
const preTabs = [
{
href: ``,
href: '',
name: 'Varo Startseite',
icon: 'heroicons:computer-desktop-20-solid'
}