39 lines
1002 B
TypeScript
39 lines
1002 B
TypeScript
import { hashPassword, isLoginData } from "$lib/Login";
|
|
import { createNewListing } from "$lib/server/modifyListing";
|
|
import { ServerCollections, writeNewListing } from "$lib/server/mongo";
|
|
import {
|
|
badRequestResponse,
|
|
forbiddenResponse,
|
|
serverErrorResponse,
|
|
singleResponse,
|
|
} from "$lib/server/responseBodies";
|
|
import type { RequestHandler } from "@sveltejs/kit";
|
|
|
|
export const POST: RequestHandler = async ({ request }): Promise<Response> => {
|
|
let body: unknown;
|
|
|
|
try {
|
|
body = await request.json();
|
|
} catch (err) {
|
|
return badRequestResponse("body is required");
|
|
}
|
|
|
|
if (!isLoginData(body)) {
|
|
return badRequestResponse("body should contain username and password");
|
|
}
|
|
|
|
if (body.role !== "player") {
|
|
return forbiddenResponse();
|
|
}
|
|
|
|
body.password = await hashPassword(body.password);
|
|
const listing = createNewListing(body);
|
|
|
|
try {
|
|
await writeNewListing(ServerCollections.Logins, listing);
|
|
return singleResponse(listing.id);
|
|
} catch (err) {
|
|
return serverErrorResponse();
|
|
}
|
|
};
|