feat: talk to the real backend, token with every WS message

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-10-28 19:26:16 +01:00
parent ffc7bfab7c
commit 673e903e65
6 changed files with 27 additions and 11 deletions

View File

@ -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

View File

@ -17,7 +17,7 @@ export type BuildingType =
| 'rally_point'
/** Why the server refuses an offer. The client shows `village.blocked.<reason>`. */
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

View File

@ -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<AuthResult>('auth/login', { email, password })).data),
signIn((await api.post<AuthResult>('auth/login', { email, password }).catch(refused)).data),
register: async (name, email, password) =>
signIn((await api.post<AuthResult>('auth/register', { name, email, password })).data),
signIn((await api.post<AuthResult>('auth/register', { name, email, password }).catch(refused)).data),
logout() {
api.setBearer(null)
client.resetConnection()

View File

@ -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.',
},
}

View File

@ -77,6 +77,7 @@ export default {
no_space: 'No free space',
max_level: 'Max level',
resources: 'Not enough resources',
requires: 'Needs a bigger headquarters',
},
},
},

View File

@ -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,
},
},
})