remove cookie auth from backend, add initial types to frontend
This commit is contained in:
parent
c6eaf49779
commit
aeb044d1a0
12 changed files with 101 additions and 21 deletions
32
frontend/src/lib/entities.ts
Normal file
32
frontend/src/lib/entities.ts
Normal 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;
|
||||
};
|
|
@ -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");
|
||||
|
|
|
@ -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) };
|
||||
}
|
||||
|
|
|
@ -8,4 +8,8 @@
|
|||
|
||||
{JSON.stringify(data.meta)}
|
||||
|
||||
{#if data.user}
|
||||
{JSON.stringify(data.user)}
|
||||
{/if}
|
||||
|
||||
<slot />
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue