Added auth to server hook.

This commit is contained in:
2025-01-23 20:58:46 -08:00
parent 8700c431cf
commit 5487a23c86
24 changed files with 1305 additions and 55 deletions

View File

@ -0,0 +1,40 @@
import { hashPassword, isLoginData } from "$lib/Login";
import { createNewListing } from "$lib/server/modifyListing";
import { writeListing } 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;
console.log("here");
try {
body = await request.json();
} catch (err) {
console.log(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 writeListing("logins", listing);
return singleResponse(listing.id);
} catch (err) {
return serverErrorResponse();
}
};