feat: fix account server api calls
Some checks failed
Build and Publish Docker Image / Build and Publish (amd64) (push) Has been cancelled
Build and Publish Docker Image / Build and Publish (arm64) (push) Has been cancelled

This commit is contained in:
mrjvs
2026-08-11 22:14:40 +02:00
parent 5655ad22ec
commit b342a595ba
7 changed files with 51 additions and 12 deletions

View File

@@ -52,10 +52,11 @@ export default defineNuxtConfig({
discordTesterRoleId: '',
discordSupporterRoleId: '',
discourseSsoSecret: '',
apiBase: 'http://localhost:8056',
apiBaseHost: 'api.pretendo.cc',
public: {
baseUrl: 'https://pretendo.network',
apiBase: 'https://api.pretendo.cc',
hcaptchaSiteKey: '',
cookieSecure: false
}

View File

@@ -2,17 +2,18 @@ import { AccountUpdateSchema } from '~~/shared/api-types';
export default defineEventHandler(async (event): Promise<void> => {
const body = await readZodBody(event, AccountUpdateSchema);
const apiFetch = useHttpApi(event);
const auth = enforceLoggedIn(event);
const apiFetch = useHttpApi(event, auth.token);
// There's no equivalent GRPC endpoint to use, so we're using the HTTP api
await apiFetch('/v1/user', {
headers: {
Authorization: `Bearer ${auth.token}`
},
body: {
method: 'POST',
body: JSON.stringify({
mii: body.mii,
environment: body.environment
}),
headers: {
'Content-type': 'application/json'
}
});
});

View File

@@ -8,6 +8,8 @@ export default defineEventHandler(async (event): Promise<GetApiAuthMe> => {
return {
pid: data.pid,
username: data.username,
accessLevel: data.accessLevel,
serverAccessLevel: data.serverAccessLevel as any,
mii: data.mii
? {
imageUrl: `https://r2-cdn.pretendo.cc/mii/${data.pid}/normal_face.png`,

View File

@@ -1,6 +1,21 @@
export function useHttpApi(event: H3Event) {
import { request } from 'undici';
import type { Dispatcher } from 'undici';
export function useHttpApi(event: H3Event, token?: string) {
const config = useRuntimeConfig(event);
return $fetch.create({
baseURL: config.public.apiBase
});
return (url: string, opts: { headers?: HeadersInit; body?: string; method?: Dispatcher.HttpMethod } = {}) => {
const headers = new Headers(opts.headers);
headers.set('Host', config.apiBaseHost); // Can't use fetch due to Host header overwriting
if (token) {
headers.set('Authorization', `Bearer ${token}`);
}
const urlWithBase = new URL(url, config.apiBase);
return request(urlWithBase, {
method: opts.method,
headers: Object.fromEntries(headers.entries()),
body: opts?.body
});
};
}

View File

@@ -3,6 +3,8 @@ import { z } from 'zod';
export type GetApiAuthMe = {
pid: number;
username: string;
accessLevel: number;
serverAccessLevel: 'dev' | 'test' | 'prod';
mii: {
imageUrl: string;
name: string;

View File

@@ -0,0 +1,17 @@
export const apiFetch = $fetch.create({
onRequest({ options }) {
const token = useAuthStore().getToken();
if (token) {
options.headers.set('Authorization', 'Bearer ' + token);
}
},
async onResponseError({ response }) {
if (response.status === 401) {
await navigateTo('/account/login');
}
}
});
export const useApiFetch = createUseFetch({
$fetch: apiFetch
});

View File

@@ -34,13 +34,14 @@ declare module 'nuxt/schema' {
discordSupporterRoleId: string;
discourseSsoSecret: string;
apiBase: string;
apiBaseHost: string;
}
interface PublicRuntimeConfig {
baseUrl: string;
apiBase: string;
cookieSecure: boolean;
hcaptchaSiteKey: string;
}
}