remove cookie auth from backend, add initial types to frontend

This commit is contained in:
sam 2024-04-05 17:35:08 +02:00
parent c6eaf49779
commit aeb044d1a0
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
12 changed files with 101 additions and 21 deletions

View file

@ -0,0 +1,32 @@
export type User = {
id: string;
name: string;
display_name: string | null;
bio: string | null;
avatar: string | null;
names: FieldEntry[];
pronouns: PronounEntry[];
fields: ProfileField[];
};
export type FieldEntry = {
value: string;
status: string;
};
export type ProfileField = {
name: string;
entries: FieldEntry[];
};
export type PronounEntry = {
value: string;
status: string;
display: string | null;
};
export type Meta = {
users: number;
members: number;
};

View file

@ -1,5 +1,8 @@
import type { Cookies, ServerLoadEvent } from "@sveltejs/kit";
export type FetchOptions = {
fetchFn?: typeof fetch;
token?: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
data?: any;
version?: number;
@ -22,13 +25,17 @@ export default async function request<T>(
path: string,
opts: FetchOptions = {},
): Promise<T> {
const { data, version, extraHeaders } = opts;
const { token, data, version, extraHeaders } = opts;
const fetchFn = opts.fetchFn ?? fetch;
const resp = await fetchFn(`/api/v${version ?? 2}${path}`, {
method,
body: data ? JSON.stringify(data) : undefined,
headers: { ...extraHeaders, "Content-Type": "application/json" },
headers: {
...extraHeaders,
...(token ? { Authorization: token } : {}),
"Content-Type": "application/json",
},
});
if (resp.status < 200 || resp.status >= 400) throw await resp.json();
@ -50,14 +57,27 @@ export async function fastRequest(
path: string,
opts: FetchOptions = {},
): Promise<void> {
const { data, version, extraHeaders } = opts;
const { token, data, version, extraHeaders } = opts;
const fetchFn = opts.fetchFn ?? fetch;
const resp = await fetchFn(`/api/v2${version ?? 2}${path}`, {
method,
body: data ? JSON.stringify(data) : undefined,
headers: { ...extraHeaders, "Content-Type": "application/json" },
headers: {
...extraHeaders,
...(token ? { Authorization: token } : {}),
"Content-Type": "application/json",
},
});
if (resp.status < 200 || resp.status >= 400) throw await resp.json();
}
/**
* Helper function to get a token from a request cookie.
* Accepts both a cookie object ({ cookies }) or a request object (req).
* @param s A Cookies or ServerLoadEvent object
* @returns A token, or `undefined` if no token is set.
*/
export const getToken = (s: Cookies | ServerLoadEvent) =>
"cookies" in s ? s.cookies.get("pronounscc-token") : s.get("pronounscc-token");

View file

@ -1,12 +1,13 @@
import request from "$lib/request";
import request, { getToken } from "$lib/request";
import type { User, Meta } from "$lib/entities";
export async function load({ fetch, cookies }) {
const meta = await request("GET", "/meta", { fetchFn: fetch });
const meta = await request<Meta>("GET", "/meta", { fetchFn: fetch });
let user;
if (cookies.get("pronounscc-token")) {
user = await request("GET", "/users/@me", { fetchFn: fetch });
user = await request<User>("GET", "/users/@me", { fetchFn: fetch, token: getToken(cookies) });
}
return { meta, user };
return { meta, user, token: getToken(cookies) };
}

View file

@ -8,4 +8,8 @@
{JSON.stringify(data.meta)}
{#if data.user}
{JSON.stringify(data.user)}
{/if}
<slot />