feat(frontend): login with center URL and consume center-issued guild tokens

This commit is contained in:
nav
2026-05-13 08:00:23 +00:00
parent 66c49ff654
commit c906cde209
10 changed files with 170 additions and 125 deletions

View File

@@ -1,14 +1,16 @@
import axios from 'axios'
import { getRuntimeConfig } from './runtime-config'
import type { AuthSession } from './auth-storage'
export type LoginPayload = { email: string; password: string }
type LoginResponse = {
centerApiBase: string
accessToken: string
refreshToken: string
tokenType: string
user: { id: string; email: string }
guilds: Array<{ nodeId: string; name: string; endpoint: string; status: 'active' | 'offline' | 'revoked' }>
guildAccessTokens: Array<{ guildNodeId: string; token: string; tokenType: string }>
}
type RefreshResponse = {
@@ -17,17 +19,14 @@ type RefreshResponse = {
tokenType: string
}
function centerClient() {
const cfg = getRuntimeConfig()
function centerClient(centerApiBase: string) {
const client = axios.create({
baseURL: cfg.centerApiBase,
baseURL: centerApiBase,
timeout: 10000,
})
client.interceptors.request.use((request) => {
const { apiKey } = getRuntimeConfig()
const requestId = crypto.randomUUID()
if (apiKey) request.headers['x-api-key'] = apiKey
request.headers['x-request-id'] = requestId
request.headers['x-client-name'] = 'fabric-frontend'
return request
@@ -36,16 +35,16 @@ function centerClient() {
return client
}
export async function loginCenter(payload: LoginPayload): Promise<AuthSession> {
const res = await centerClient().post<LoginResponse>('/auth/login', payload)
export async function loginCenter(centerApiBase: string, payload: LoginPayload): Promise<AuthSession> {
const res = await centerClient(centerApiBase).post<LoginResponse>('/auth/login', payload)
return { ...res.data, centerApiBase }
}
export async function refreshCenter(centerApiBase: string, refreshToken: string): Promise<RefreshResponse> {
const res = await centerClient(centerApiBase).post<RefreshResponse>('/auth/refresh', { refreshToken })
return res.data
}
export async function refreshCenter(refreshToken: string): Promise<RefreshResponse> {
const res = await centerClient().post<RefreshResponse>('/auth/refresh', { refreshToken })
return res.data
}
export async function logoutCenter(refreshToken: string): Promise<void> {
await centerClient().post('/auth/logout', { refreshToken })
export async function logoutCenter(centerApiBase: string, refreshToken: string): Promise<void> {
await centerClient(centerApiBase).post('/auth/logout', { refreshToken })
}