diff --git a/src/handler.ts b/src/handler.ts index 8d40bfb9..fb70fcc8 100644 --- a/src/handler.ts +++ b/src/handler.ts @@ -12,6 +12,9 @@ import { getUserBySearch } from "@/routes/user/getUsersBySearch"; import { allGames } from "@/routes/games/allGames"; import { getAssetFromId } from "@/routes/asset/getAssetFromId"; import { createNotFoundResponse } from "@/lib/helpers/responses/notFoundResponse"; +import { login } from "@/routes/auth/login"; +import { logout } from "@/routes/auth/logout"; +import { signup } from "@/routes/auth/signup"; const router = Router(); @@ -27,6 +30,9 @@ router .get("/oc-generators", errorHandler(getGenerators)) .get("/oc-generator/:gameId", errorHandler(getGenerator)) .get("/discord/contributors", errorHandler(getContributors)) + .post("/auth/login", errorHandler(login)) + .post("/auth/logout", errorHandler(logout)) + .post("/auth/signup", errorHandler(signup)) .all("*", (): Response => { return createNotFoundResponse("Route doesn't exist", responseHeaders); }); diff --git a/src/lib/helpers/responses/notFoundResponse.ts b/src/lib/helpers/responses/notFoundResponse.ts index 3adc6977..14768c3c 100644 --- a/src/lib/helpers/responses/notFoundResponse.ts +++ b/src/lib/helpers/responses/notFoundResponse.ts @@ -8,6 +8,7 @@ export function createNotFoundResponse(errorMessage, responseHeaders) { }; return new Response(JSON.stringify(responseBody), { + status: 404, headers: responseHeaders, }); } diff --git a/src/routes/auth/login.ts b/src/routes/auth/login.ts index 2e5db1bf..6eed9c99 100644 --- a/src/routes/auth/login.ts +++ b/src/routes/auth/login.ts @@ -1,6 +1,7 @@ import { auth } from "@/lib/auth/lucia"; import type { LoginBody } from "@/lib/types/auth"; import { responseHeaders } from "@/lib/responseHeaders"; +import "lucia/polyfill/node" // required for old node versions export const login = async (request: Request): Promise => { const body = (await request.json()) as LoginBody; @@ -14,6 +15,7 @@ export const login = async (request: Request): Promise => { error: "400 Bad Request", }), { + status: 400, headers: responseHeaders, } ); diff --git a/src/routes/auth/signup.ts b/src/routes/auth/signup.ts index 6bbac10d..0b59bad3 100644 --- a/src/routes/auth/signup.ts +++ b/src/routes/auth/signup.ts @@ -2,7 +2,7 @@ import { auth } from "@/lib/auth/lucia"; import type { RegisterBody } from "@/lib/types/auth"; import { responseHeaders } from "@/lib/responseHeaders"; -export const login = async (request: Request): Promise => { +export const signup = async (request: Request): Promise => { const body = (await request.json()) as RegisterBody; const { username, password, email, passwordConfirm } = body; @@ -14,6 +14,7 @@ export const login = async (request: Request): Promise => { error: "400 Bad Request", }), { + status: 400, headers: responseHeaders, } ); diff --git a/src/routes/auth/validate.ts b/src/routes/auth/validate.ts new file mode 100644 index 00000000..95e7373e --- /dev/null +++ b/src/routes/auth/validate.ts @@ -0,0 +1,32 @@ +import { auth } from "@/lib/auth/lucia"; +import { responseHeaders } from "@/lib/responseHeaders"; + +export const validate = async (request: Request): Promise => { + const authRequest = auth.handleRequest(request); + const session = await authRequest.validate(); + + if (!session) { + return new Response( + JSON.stringify({ + success: false, + status: "error", + error: "401 Unauthorized", + }), + { + status: 401, + headers: responseHeaders, + } + ); + } + + return new Response( + JSON.stringify({ + success: true, + status: "ok", + session, + }), + { + headers: responseHeaders, + } + ); +}; diff --git a/src/routes/discord/contributors.ts b/src/routes/discord/contributors.ts index 5259c517..843da63a 100644 --- a/src/routes/discord/contributors.ts +++ b/src/routes/discord/contributors.ts @@ -65,6 +65,7 @@ export const getContributors = async ( contributors: members, }), { + status: 200, headers: responseHeaders, } ); diff --git a/src/routes/games/allGames.ts b/src/routes/games/allGames.ts index cc4ad0fe..6444e5c5 100644 --- a/src/routes/games/allGames.ts +++ b/src/routes/games/allGames.ts @@ -43,6 +43,7 @@ export const allGames = async ( results: gameList, }), { + status: 200, headers: responseHeaders, } ); diff --git a/src/routes/oc-generators/getGenerator.ts b/src/routes/oc-generators/getGenerator.ts index b1c8c0e4..cabd9aed 100644 --- a/src/routes/oc-generators/getGenerator.ts +++ b/src/routes/oc-generators/getGenerator.ts @@ -37,6 +37,7 @@ export const getGenerator = async ( data: generatorData, }), { + status: 200, headers: responseHeaders, } ); diff --git a/src/routes/search/search.ts b/src/routes/search/search.ts index 1daff116..a8d5341c 100644 --- a/src/routes/search/search.ts +++ b/src/routes/search/search.ts @@ -114,6 +114,7 @@ export const getRecentAssets = async ( results, }), { + status: 200, headers: responseHeaders, } ); diff --git a/src/routes/user/getUserByUsername.ts b/src/routes/user/getUserByUsername.ts index 071c8680..55638b41 100644 --- a/src/routes/user/getUserByUsername.ts +++ b/src/routes/user/getUserByUsername.ts @@ -71,6 +71,7 @@ export const getUserByUsername = async ( uploadedAssets, }), { + status: 200, headers: responseHeaders, } ); diff --git a/src/routes/user/getUsersBySearch.ts b/src/routes/user/getUsersBySearch.ts index 8e916ae4..9b7c48d9 100644 --- a/src/routes/user/getUsersBySearch.ts +++ b/src/routes/user/getUsersBySearch.ts @@ -51,6 +51,7 @@ export const getUserBySearch = async ( results: results, }), { + status: 200, headers: responseHeaders, } ); diff --git a/src/worker-configuration.d.ts b/src/worker-configuration.d.ts index 5f5b5a02..7064f5ee 100644 --- a/src/worker-configuration.d.ts +++ b/src/worker-configuration.d.ts @@ -2,4 +2,5 @@ interface Env { DISCORD_TOKEN: string; bucket: R2Bucket; database: D1Database; + DATABASE_URL: string; }