Skip to content

Commit

Permalink
fix: ensure no other owners exist in setup endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
johanohly committed Jan 29, 2025
1 parent d6a1360 commit 1cb3535
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/routes/api/users/setup/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,29 @@ import {
} from '$lib/server/utils/auth';
import { hashArgon2 } from '$lib/server/utils/hash';
import { signUpSchema } from '$lib/zod/auth';
import { db } from '$lib/db';

export const POST: RequestHandler = async ({ cookies, request }) => {
const form = await superValidate(request, zod(signUpSchema));
if (!form.valid) {
return actionResult('failure', { form });
}

const owners = await db
.selectFrom('user')
.where('role', '=', 'owner')
.selectAll()
.execute();
if (owners.length > 0) {
form.message = { type: 'error', text: 'Owner already exists' };
return actionResult('failure', { form });
}

const { username, password, displayName, unit } = form.data;
const exists = await usernameExists(username);
if (exists) {
setError(form, 'username', 'Username already exists');
return actionResult('failure', { form });
}

const userId = generateId(15);
Expand Down

0 comments on commit 1cb3535

Please sign in to comment.