Commit 0a8faa53 authored by ThinhNC's avatar ThinhNC

fix(fe/client): sync auth tokens with localStorage and configure dynamic VITE_API_URL

parent 18d622d4
import axios from "axios";
import { useAuthStore } from "@/stores/auth-store";
const apiBaseUrl = import.meta.env.VITE_API_URL || "http://localhost:7777/api/v1";
// Create an Axios instance
export const apiClient = axios.create({
baseURL: "http://localhost:7777/api/v1",
baseURL: apiBaseUrl,
timeout: 10000,
withCredentials: true,
headers: {
......@@ -13,7 +15,7 @@ export const apiClient = axios.create({
// A separate instance for refreshing tokens to avoid interceptor loops
const refreshClient = axios.create({
baseURL: "http://localhost:7777/api/v1",
baseURL: apiBaseUrl,
timeout: 10000,
withCredentials: true,
headers: {
......@@ -80,8 +82,11 @@ apiClient.interceptors.response.use(
isRefreshing = true;
try {
// Try refreshing the token (cookies will be sent automatically)
const response = await refreshClient.post("/auth/refresh", {});
const storedRefreshToken = useAuthStore.getState().refreshToken || localStorage.getItem("refreshToken");
// Try refreshing the token (cookies will be sent automatically, body as fallback for mobile webviews)
const response = await refreshClient.post("/auth/refresh", {
refreshToken: storedRefreshToken || undefined,
});
if (response.data?.success) {
const newData = response.data?.data;
......
......@@ -74,7 +74,11 @@ const LoginPage: React.FC = () => {
});
if (response.success && response.data) {
updateRememberedEmail(values.email, values.rememberMe);
setAuth(response.data.user, "", "");
setAuth(
response.data.user,
response.data.accessToken || "",
response.data.refreshToken || ""
);
openSnackbar({
type: "success",
text: t("auth.login.success"),
......
......@@ -12,7 +12,7 @@ export const authService = {
return response.data;
},
async login(data: LoginRequest): Promise<ApiResponse<{ user: User }>> {
async login(data: LoginRequest): Promise<ApiResponse<{ user: User; accessToken?: string; refreshToken?: string }>> {
const response = await apiClient.post("/auth/login", data);
return response.data;
},
......
......@@ -9,7 +9,7 @@ import {
function ensureSuccess<T extends { success: boolean; message?: string }>(response: T): T {
if (!response.success) {
throw new Error(response.message || "Yêu cầu quản lý giao dịch thất bại");
throw new Error(response.message || "Transaction request failed");
}
return response;
}
......
......@@ -10,7 +10,7 @@ import {
function ensureSuccess<T extends { success: boolean; message?: string }>(response: T): T {
if (!response.success) {
throw new Error(response.message || "Yêu cầu quản lý ví không thành công");
throw new Error(response.message || "Wallet request failed");
}
return response;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment