Skip to content

Commit

Permalink
add status code into responses
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Aug 1, 2023
1 parent ad4e035 commit 1bb24e7
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);
});
Expand Down
1 change: 1 addition & 0 deletions src/lib/helpers/responses/notFoundResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export function createNotFoundResponse(errorMessage, responseHeaders) {
};

return new Response(JSON.stringify(responseBody), {
status: 404,
headers: responseHeaders,
});
}
2 changes: 2 additions & 0 deletions src/routes/auth/login.ts
Original file line number Diff line number Diff line change
@@ -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<Response> => {
const body = (await request.json()) as LoginBody;
Expand All @@ -14,6 +15,7 @@ export const login = async (request: Request): Promise<Response> => {
error: "400 Bad Request",
}),
{
status: 400,
headers: responseHeaders,
}
);
Expand Down
3 changes: 2 additions & 1 deletion src/routes/auth/signup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Response> => {
export const signup = async (request: Request): Promise<Response> => {
const body = (await request.json()) as RegisterBody;
const { username, password, email, passwordConfirm } = body;

Expand All @@ -14,6 +14,7 @@ export const login = async (request: Request): Promise<Response> => {
error: "400 Bad Request",
}),
{
status: 400,
headers: responseHeaders,
}
);
Expand Down
32 changes: 32 additions & 0 deletions src/routes/auth/validate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { auth } from "@/lib/auth/lucia";
import { responseHeaders } from "@/lib/responseHeaders";

export const validate = async (request: Request): Promise<Response> => {
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,
}
);
};
1 change: 1 addition & 0 deletions src/routes/discord/contributors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const getContributors = async (
contributors: members,
}),
{
status: 200,
headers: responseHeaders,
}
);
Expand Down
1 change: 1 addition & 0 deletions src/routes/games/allGames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const allGames = async (
results: gameList,
}),
{
status: 200,
headers: responseHeaders,
}
);
Expand Down
1 change: 1 addition & 0 deletions src/routes/oc-generators/getGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const getGenerator = async (
data: generatorData,
}),
{
status: 200,
headers: responseHeaders,
}
);
Expand Down
1 change: 1 addition & 0 deletions src/routes/search/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export const getRecentAssets = async (
results,
}),
{
status: 200,
headers: responseHeaders,
}
);
Expand Down
1 change: 1 addition & 0 deletions src/routes/user/getUserByUsername.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export const getUserByUsername = async (
uploadedAssets,
}),
{
status: 200,
headers: responseHeaders,
}
);
Expand Down
1 change: 1 addition & 0 deletions src/routes/user/getUsersBySearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const getUserBySearch = async (
results: results,
}),
{
status: 200,
headers: responseHeaders,
}
);
Expand Down
1 change: 1 addition & 0 deletions src/worker-configuration.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ interface Env {
DISCORD_TOKEN: string;
bucket: R2Bucket;
database: D1Database;
DATABASE_URL: string;
}

0 comments on commit 1bb24e7

Please sign in to comment.