fix: pass refresh_token in body when calling /auth/refresh

The refresh endpoint expects {"refresh_token":"..."} in the POST body,
but the frontend was sending an empty body (EOF error). Now stores the
refresh_token from login response and sends it on refresh requests.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Chever John 2026-06-30 01:28:45 +08:00
parent df0d490a42
commit 9ecf3c3179
No known key found for this signature in database
GPG Key ID: 2894D52ED1211DF0
2 changed files with 10 additions and 1 deletions

View File

@ -1,4 +1,4 @@
import { http, setAccessToken } from './client';
import { http, setAccessToken, setRefreshToken } from './client';
import type { Profile, Company, UserRole } from '../types';
export interface LoginRequest {
@ -31,6 +31,7 @@ export const authApi = {
login: async (data: LoginRequest) => {
const res = await http.post<LoginResponse>('/auth/login', data);
setAccessToken(res.data.access_token);
setRefreshToken(res.data.refresh_token);
return res.data;
},
@ -40,6 +41,7 @@ export const authApi = {
logout: async () => {
await http.post('/auth/logout');
setAccessToken(null);
setRefreshToken(null);
},
refresh: async () => {

View File

@ -13,12 +13,17 @@ export interface PaginationParams {
const API_BASE = '/api/v1';
let accessToken: string | null = null;
let refreshTokenValue: string | null = null;
let refreshPromise: Promise<boolean> | null = null;
export function setAccessToken(token: string | null) {
accessToken = token;
}
export function setRefreshToken(token: string | null) {
refreshTokenValue = token;
}
export function getAccessToken(): string | null {
return accessToken;
}
@ -27,6 +32,8 @@ async function refreshToken(): Promise<boolean> {
try {
const res = await fetch(`${API_BASE}/auth/refresh`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ refresh_token: refreshTokenValue }),
credentials: 'include',
});
if (!res.ok) return false;