From 673e903e6505665d59afef39956a4258a6f71b39 Mon Sep 17 00:00:00 2001 From: "Fabian @ Blax Software" Date: Wed, 28 Oct 2026 19:26:16 +0100 Subject: [PATCH] feat: talk to the real backend, token with every WS message Co-Authored-By: Claude Opus 5.5 --- .env.example | 6 +++--- app/types/game.ts | 2 +- app/utils/networking.ts | 21 +++++++++++++++++---- i18n/locales/en.ts | 2 ++ i18n/pages/village.ts | 1 + nuxt.config.ts | 6 +++--- 6 files changed, 27 insertions(+), 11 deletions(-) diff --git a/.env.example b/.env.example index cc1a98f..9f6c428 100644 --- a/.env.example +++ b/.env.example @@ -2,6 +2,6 @@ NUXT_PUBLIC_SERVER_URL=https://server-village.blax.at NUXT_PUBLIC_WEBS_URL=ws-server-village.blax.at NUXT_PUBLIC_WS_PROTOCOL=wss -NUXT_PUBLIC_PUSHER_APP_KEY=websocket -# true until the backend serves the WS API (see CLAUDE.md, "Mock server"). -NUXT_PUBLIC_MOCK_SERVER=true +NUXT_PUBLIC_PUSHER_APP_KEY=village-politics +# true swaps the backend for the in-browser fake in app/mock/ (see CLAUDE.md, "Mock server"). +NUXT_PUBLIC_MOCK_SERVER=false diff --git a/app/types/game.ts b/app/types/game.ts index 82c13fb..efe7008 100644 --- a/app/types/game.ts +++ b/app/types/game.ts @@ -17,7 +17,7 @@ export type BuildingType = | 'rally_point' /** Why the server refuses an offer. The client shows `village.blocked.`. */ -export type BlockedReason = 'max_per_village' | 'no_space' | 'max_level' +export type BlockedReason = 'max_per_village' | 'no_space' | 'max_level' | 'requires' export interface AuthUser { id: number diff --git a/app/utils/networking.ts b/app/utils/networking.ts index dce37b8..c7cdc84 100644 --- a/app/utils/networking.ts +++ b/app/utils/networking.ts @@ -1,6 +1,6 @@ import { createApiClient, type WsClientConfig } from '@blax-software/networking' import { createVueWsClient, type VueWsClient } from '@blax-software/networking/vue' -import type { AuthResult } from '~/types/game' +import type { AuthResult, GameError } from '~/types/game' import { createMockServer } from '~/mock/server' /** The part of the WS client the game uses. The mock server implements the same surface. */ @@ -40,7 +40,8 @@ export function setupNetworking(options: NetworkingOptions) { return } - const api = createApiClient({ serverUrl: options.serverUrl, notify: options.notify }) + // Bearer token only, no cookies, so the API can allow any origin (web and Steam). + const api = createApiClient({ serverUrl: options.serverUrl, notify: options.notify, withCredentials: false }) api.loadBearerFromStorage() const client = createVueWsClient({ @@ -52,6 +53,13 @@ export function setupNetworking(options: NetworkingOptions) { sendTimeout: 15000, rejectOnDisconnect: true, }) + + // The WS server authenticates a socket from `authtoken` in the message data (as in + // learn-atc), so every send carries the bearer token. + const rawSend = client.send.bind(client) + client.send = ((event: string, data: object = {}, channel?: string | null, progress?: any) => + rawSend(event, { ...data, authtoken: api.bearerToken || undefined }, channel, progress)) as typeof client.send + client.setAppReady() ws = client @@ -61,12 +69,17 @@ export function setupNetworking(options: NetworkingOptions) { return result } + // Pass the server's `{ code, message }` on, like a refused WS intent. + const refused = (e: any): never => { + throw { message: e?.response?.data?.message ?? e?.message, code: e?.response?.data?.code } satisfies GameError + } + gameAuth = { token: () => api.bearerToken || null, login: async (email, password) => - signIn((await api.post('auth/login', { email, password })).data), + signIn((await api.post('auth/login', { email, password }).catch(refused)).data), register: async (name, email, password) => - signIn((await api.post('auth/register', { name, email, password })).data), + signIn((await api.post('auth/register', { name, email, password }).catch(refused)).data), logout() { api.setBearer(null) client.resetConnection() diff --git a/i18n/locales/en.ts b/i18n/locales/en.ts index 76ca9df..64926f4 100644 --- a/i18n/locales/en.ts +++ b/i18n/locales/en.ts @@ -39,5 +39,7 @@ export default { max_level: 'Already at max level.', resources: 'Not enough resources.', core_building: 'Core buildings cannot go below level 0.', + requires: 'Build its requirements first.', + world_full: 'This world is full.', }, } diff --git a/i18n/pages/village.ts b/i18n/pages/village.ts index bf392eb..9dd50f6 100644 --- a/i18n/pages/village.ts +++ b/i18n/pages/village.ts @@ -77,6 +77,7 @@ export default { no_space: 'No free space', max_level: 'Max level', resources: 'Not enough resources', + requires: 'Needs a bigger headquarters', }, }, }, diff --git a/nuxt.config.ts b/nuxt.config.ts index 5808161..095c6c7 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -38,9 +38,9 @@ export default defineNuxtConfig({ SERVER_URL: 'https://server-village.localhost.at', WEBS_URL: 'ws-server-village.localhost.at', WS_PROTOCOL: 'wss', - PUSHER_APP_KEY: 'websocket', - // Until the backend exists, an in-browser fake answers the same WS intents (app/mock/). - MOCK_SERVER: true, + PUSHER_APP_KEY: 'village-politics', + // true swaps the backend for the in-browser fake in app/mock/ (UI work without a backend). + MOCK_SERVER: false, }, }, })