add option to add user manually (#22)
All checks were successful
delpoy / build-and-deploy (push) Successful in 1m25s

This commit is contained in:
2023-12-23 00:41:49 +01:00
parent 7878fef301
commit b92e494ecb
4 changed files with 223 additions and 2 deletions

View File

@@ -1,8 +1,9 @@
import { getSession } from '$lib/server/session';
import { Permissions } from '$lib/permissions';
import type { RequestHandler } from '@sveltejs/kit';
import { error, type RequestHandler } from '@sveltejs/kit';
import { User } from '$lib/server/database';
import { type Attributes, Op } from 'sequelize';
import { ApiError, getJavaUuid, getNoAuthUuid, UserNotFoundError } from '$lib/server/minecraft';
export const POST = (async ({ request, cookies }) => {
if (getSession(cookies, { permissions: [Permissions.UserRead] }) == null) {
@@ -88,6 +89,90 @@ export const PATCH = (async ({ request, cookies }) => {
return new Response();
}) satisfies RequestHandler;
export const PUT = (async ({ request, cookies }) => {
if (getSession(cookies, { permissions: [Permissions.UserWrite] }) == null) {
return new Response(null, {
status: 401
});
}
const data: {
firstname: string;
lastname: string;
birthday: string;
telephone: string;
username: string;
playertype: string;
} = await request.json();
if (
data.firstname == null ||
data.lastname == null ||
data.birthday == null ||
data.username == null ||
data.playertype == null
) {
return new Response(null, { status: 400 });
}
let uuid: string | null;
try {
switch (data.playertype) {
case 'java':
uuid = await getJavaUuid(data.username);
break;
case 'bedrock':
uuid = null;
// uuid = await getBedrockUuid(username);
break;
case 'noauth':
uuid = getNoAuthUuid(data.username);
break;
default:
throw new Error(`invalid player type (${data.playertype})`);
}
} catch (e) {
if (e instanceof UserNotFoundError) {
throw error(400, `Der Spielername ${data.username} existiert nicht`);
} else if (e instanceof ApiError) {
console.error((e as Error).message);
uuid = null;
} else {
console.error((e as Error).message);
throw error(500);
}
}
if (uuid && (await User.findOne({ where: { uuid: uuid } }))) {
throw error(400, 'Dieser Minecraft-Account wurde bereits registriert');
} else if (
await User.findOne({
where: {
firstname: data.firstname,
lastname: data.lastname,
birthday: new Date(data.birthday).toUTCString()
}
})
) {
throw error(400, 'Ein Nutzer mit demselben Namen und Geburtstag wurde bereits registriert');
}
await User.create({
firstname: data.firstname,
lastname: data.lastname,
birthday: new Date(data.birthday).toUTCString(),
telephone: data.telephone,
username: data.username,
playertype: data.playertype,
password: null,
uuid: uuid
});
return new Response();
}) satisfies RequestHandler;
export const DELETE = (async ({ request, cookies }) => {
if (getSession(cookies, { permissions: [Permissions.UserWrite] }) == null) {
return new Response(null, {