Skip to content

Commit

Permalink
Fixed create user
Browse files Browse the repository at this point in the history
  • Loading branch information
Liam Boddin committed Sep 16, 2023
1 parent 59dbad9 commit 9f68ece
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion Server/src/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import CryptoService from "./crypto.service"
import database from "./database.service"
import { z } from "zod"
import { HTTPError } from "../models/error"
import { PrismaClientKnownRequestError } from "@prisma/client/runtime/library"

/**
* Service for user management
Expand All @@ -18,7 +19,16 @@ export default class UserService {
* @throws HTTPError if password could not be hashed or PrismaError if user could not be created
*/
public static async createUser(name: string, password: string): Promise<User> {
await database.users.getByUsername(name)
// Pre-check whether a user with that username already exists, so we expect a prisma error to be thrown
try {
await database.users.getByUsername(name)
throw new HTTPError("The user does already exist", 409)
} catch (err) {
// Throw if any other error than expected 'not found errors' are thrown
if (!(err instanceof PrismaClientKnownRequestError) || !["P2001", "P2018", "P2021", "P2022", "P2025"].includes(err.code)) {
throw err
}
}

const hashed_pass: string = await CryptoService.produceHash(password)

Expand Down

0 comments on commit 9f68ece

Please sign in to comment.