mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-07-18 16:43:56 -05:00
Add auth to public API
This commit is contained in:
parent
2699d1460e
commit
58bbdccee8
11
app/features/api-public/api-public-utils.server.ts
Normal file
11
app/features/api-public/api-public-utils.server.ts
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
const apiTokens = process.env["PUBLIC_API_TOKENS"]?.split(",") ?? [];
|
||||
export function requireBearerAuth(req: Request) {
|
||||
const authHeader = req.headers.get("Authorization");
|
||||
if (!authHeader) {
|
||||
throw new Response("Missing Authorization header", { status: 401 });
|
||||
}
|
||||
const token = authHeader.replace("Bearer ", "");
|
||||
if (!apiTokens.includes(token)) {
|
||||
throw new Response("Invalid token", { status: 401 });
|
||||
}
|
||||
}
|
||||
|
|
@ -7,12 +7,15 @@ import type { GetTournamentMatchResponse } from "../schema";
|
|||
import { i18next } from "~/modules/i18n";
|
||||
import { jsonArrayFrom } from "kysely/helpers/sqlite";
|
||||
import { resolveMapList } from "~/features/tournament-bracket/core/mapList.server";
|
||||
import { requireBearerAuth } from "../api-public-utils.server";
|
||||
|
||||
const paramsSchema = z.object({
|
||||
id,
|
||||
});
|
||||
|
||||
export const loader = async ({ params }: LoaderFunctionArgs) => {
|
||||
export const loader = async ({ params, request }: LoaderFunctionArgs) => {
|
||||
requireBearerAuth(request);
|
||||
|
||||
const t = await i18next.getFixedT("en", ["game-misc"]);
|
||||
const { id } = parseParams({
|
||||
params,
|
||||
|
|
|
|||
|
|
@ -7,12 +7,15 @@ import type { GetTournamentTeamsResponse } from "../schema";
|
|||
import { databaseTimestampToDate } from "~/utils/dates";
|
||||
import { jsonArrayFrom } from "kysely/helpers/sqlite";
|
||||
import { i18next } from "~/modules/i18n";
|
||||
import { requireBearerAuth } from "../api-public-utils.server";
|
||||
|
||||
const paramsSchema = z.object({
|
||||
id,
|
||||
});
|
||||
|
||||
export const loader = async ({ params }: LoaderFunctionArgs) => {
|
||||
export const loader = async ({ params, request }: LoaderFunctionArgs) => {
|
||||
requireBearerAuth(request);
|
||||
|
||||
const t = await i18next.getFixedT("en", ["game-misc"]);
|
||||
const { id } = parseParams({
|
||||
params,
|
||||
|
|
|
|||
|
|
@ -7,12 +7,15 @@ import type { GetTournamentResponse } from "../schema";
|
|||
import { databaseTimestampToDate } from "~/utils/dates";
|
||||
import { HACKY_resolvePicture } from "~/features/tournament/tournament-utils";
|
||||
import { jsonArrayFrom } from "kysely/helpers/sqlite";
|
||||
import { requireBearerAuth } from "../api-public-utils.server";
|
||||
|
||||
const paramsSchema = z.object({
|
||||
id,
|
||||
});
|
||||
|
||||
export const loader = async ({ params }: LoaderFunctionArgs) => {
|
||||
export const loader = async ({ params, request }: LoaderFunctionArgs) => {
|
||||
requireBearerAuth(request);
|
||||
|
||||
const { id } = parseParams({ params, schema: paramsSchema });
|
||||
|
||||
const tournament = notFoundIfFalsy(
|
||||
|
|
|
|||
|
|
@ -6,12 +6,15 @@ import { notFoundIfFalsy, parseParams } from "~/utils/remix";
|
|||
import type { GetUserResponse } from "../schema";
|
||||
import { jsonArrayFrom } from "kysely/helpers/sqlite";
|
||||
import { i18next } from "~/modules/i18n";
|
||||
import { requireBearerAuth } from "../api-public-utils.server";
|
||||
|
||||
const paramsSchema = z.object({
|
||||
identifier: z.string(),
|
||||
});
|
||||
|
||||
export const loader = async ({ params }: LoaderFunctionArgs) => {
|
||||
export const loader = async ({ params, request }: LoaderFunctionArgs) => {
|
||||
requireBearerAuth(request);
|
||||
|
||||
const t = await i18next.getFixedT("en", ["weapons"]);
|
||||
const { identifier } = parseParams({ params, schema: paramsSchema });
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user