From 39ac71e21053c6ef0cc444b4573b18791165a200 Mon Sep 17 00:00:00 2001 From: Barcode Betty Date: Mon, 30 Mar 2026 11:00:52 +0000 Subject: [PATCH] fix: align frontend auth with API token response contract - Register sends display_name instead of name - Register/Login handle TokenResponse (access_token, not token) - Fetch /auth/me after register/login to populate user object Co-Authored-By: Paperclip --- src/pages/Login.tsx | 15 +++++++++++++-- src/pages/Register.tsx | 15 +++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/pages/Login.tsx b/src/pages/Login.tsx index d29b0f1..3542455 100644 --- a/src/pages/Login.tsx +++ b/src/pages/Login.tsx @@ -5,6 +5,13 @@ import { api } from '../lib/api.ts' import { mockUser } from '../lib/mock-data.ts' import type { User } from '../types/api.ts' +interface TokenResponse { + access_token: string + refresh_token: string + token_type: string + expires_in: number +} + export function Login() { const [email, setEmail] = useState('') const [password, setPassword] = useState('') @@ -24,8 +31,12 @@ export function Login() { setLoading(true) try { - const res = await api.post<{ user: User; token: string }>('/auth/login', { email, password }) - setAuth(res.user, res.token) + const res = await api.post('/auth/login', { email, password }) + const userRes = await fetch(`${import.meta.env.VITE_API_URL ?? '/api/v1'}/auth/me`, { + headers: { Authorization: `Bearer ${res.access_token}` }, + }) + const user = (await userRes.json()) as User + setAuth(user, res.access_token) navigate('/') } catch { if (import.meta.env.VITE_MOCK_AUTH === 'true') { diff --git a/src/pages/Register.tsx b/src/pages/Register.tsx index 49bcffc..e765706 100644 --- a/src/pages/Register.tsx +++ b/src/pages/Register.tsx @@ -5,6 +5,13 @@ import { api } from '../lib/api.ts' import { mockUser } from '../lib/mock-data.ts' import type { User } from '../types/api.ts' +interface TokenResponse { + access_token: string + refresh_token: string + token_type: string + expires_in: number +} + export function Register() { const [name, setName] = useState('') const [email, setEmail] = useState('') @@ -30,8 +37,12 @@ export function Register() { setLoading(true) try { - const res = await api.post<{ user: User; token: string }>('/auth/register', { name, email, password }) - setAuth(res.user, res.token) + const res = await api.post('/auth/register', { display_name: name, email, password }) + const userRes = await fetch(`${import.meta.env.VITE_API_URL ?? '/api/v1'}/auth/me`, { + headers: { Authorization: `Bearer ${res.access_token}` }, + }) + const user = (await userRes.json()) as User + setAuth(user, res.access_token) navigate('/') } catch { if (import.meta.env.VITE_MOCK_AUTH === 'true') {