implemented basic user registration
This commit is contained in:
parent
8c03efbfc0
commit
1d6ca4abd8
|
|
@ -0,0 +1,6 @@
|
||||||
|
|
||||||
|
export class ArgumentError extends Error {
|
||||||
|
constructor(message: string) {
|
||||||
|
super(message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
import { PrismaClient } from "@prisma/client"
|
||||||
|
|
||||||
|
const prisma = new PrismaClient()
|
||||||
|
|
||||||
|
export default prisma
|
||||||
|
|
@ -0,0 +1,43 @@
|
||||||
|
import Bun from "bun"
|
||||||
|
|
||||||
|
import { ArgumentError } from "$lib/errors"
|
||||||
|
|
||||||
|
import Log from "$lib/server/log"
|
||||||
|
import db from "$lib/server/database"
|
||||||
|
|
||||||
|
class UserMgmt {
|
||||||
|
|
||||||
|
async register(email: string, password: string, display_name: string) {
|
||||||
|
if (email.length == 0 || password.length == 0 || display_name.length == 0) {
|
||||||
|
throw new ArgumentError("No field may be empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
const user = await db.user.create({
|
||||||
|
data: {
|
||||||
|
email: email,
|
||||||
|
password_hash: await Bun.password.hash(password, "argon2id"),
|
||||||
|
display_name: display_name
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
Log.info(Log.module.USER, `Created user with id ${user.id}`)
|
||||||
|
console.log(user)
|
||||||
|
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
async login(email: string, password: string) {
|
||||||
|
const user = await db.user.findUnique({
|
||||||
|
where: {
|
||||||
|
email: email
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
const _manager = new UserMgmt()
|
||||||
|
|
||||||
|
export default _manager
|
||||||
|
|
@ -0,0 +1,41 @@
|
||||||
|
import type { RequestHandler } from "./$types"
|
||||||
|
|
||||||
|
import { json, error } from "@sveltejs/kit"
|
||||||
|
|
||||||
|
import UserMgmt from "$lib/server/usermgmt"
|
||||||
|
|
||||||
|
import Util from "$lib/util"
|
||||||
|
|
||||||
|
export const POST: RequestHandler = async ({ request }) => {
|
||||||
|
|
||||||
|
const data = await request.formData()
|
||||||
|
|
||||||
|
const email = data.get("email")
|
||||||
|
const password = data.get("password")
|
||||||
|
const display_name = data.get("display_name")
|
||||||
|
|
||||||
|
if (data.keys.length > 3 || !(typeof email === "string" && typeof password === "string" && typeof display_name === "string")) {
|
||||||
|
return error(400, { message: "Invalid field arguments" })
|
||||||
|
}
|
||||||
|
if (email.length == 0) {
|
||||||
|
return error(400, { message: "email must not be empty" })
|
||||||
|
}
|
||||||
|
if (password.length == 0) {
|
||||||
|
return error(400, { message: "password must not be empty" })
|
||||||
|
}
|
||||||
|
if (display_name.length == 0) {
|
||||||
|
return error(400, { message: "Display name must not be empty" })
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Util.check_email_format(email)) {
|
||||||
|
return error(400, { message: "invalid email format" })
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const user = UserMgmt.register(email, password, display_name)
|
||||||
|
} catch (e) {
|
||||||
|
return error(500)
|
||||||
|
}
|
||||||
|
|
||||||
|
return json({})
|
||||||
|
}
|
||||||
|
|
@ -1,10 +1,17 @@
|
||||||
import type { Actions } from "./$types";
|
import type { Actions } from "./$types";
|
||||||
|
|
||||||
export const actions = {
|
export const actions = {
|
||||||
register: async ({ request }) => {
|
register: async ({ request, fetch }) => {
|
||||||
|
|
||||||
console.log("register")
|
console.log("register")
|
||||||
|
|
||||||
|
const result = await fetch("/api/register", {
|
||||||
|
method: "POST",
|
||||||
|
body: await request.formData()
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log(result)
|
||||||
|
|
||||||
return { message: "Erfolg" }
|
return { message: "Erfolg" }
|
||||||
}
|
}
|
||||||
} satisfies Actions
|
} satisfies Actions
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue