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 <noreply@paperclip.ing>
This commit is contained in:
+13
-2
@@ -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<TokenResponse>('/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') {
|
||||
|
||||
+13
-2
@@ -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<TokenResponse>('/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') {
|
||||
|
||||
Reference in New Issue
Block a user