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 axios from "axios";
import { useAuthStore } from "@/stores/auth-store"; import { useAuthStore } from "@/stores/auth-store";
const apiBaseUrl = import.meta.env.VITE_API_URL || "http://localhost:7777/api/v1";
// Create an Axios instance // Create an Axios instance
export const apiClient = axios.create({ export const apiClient = axios.create({
baseURL: "http://localhost:7777/api/v1", baseURL: apiBaseUrl,
timeout: 10000, timeout: 10000,
withCredentials: true, withCredentials: true,
headers: { headers: {
...@@ -13,7 +15,7 @@ export const apiClient = axios.create({ ...@@ -13,7 +15,7 @@ export const apiClient = axios.create({
// A separate instance for refreshing tokens to avoid interceptor loops // A separate instance for refreshing tokens to avoid interceptor loops
const refreshClient = axios.create({ const refreshClient = axios.create({
baseURL: "http://localhost:7777/api/v1", baseURL: apiBaseUrl,
timeout: 10000, timeout: 10000,
withCredentials: true, withCredentials: true,
headers: { headers: {
...@@ -80,8 +82,11 @@ apiClient.interceptors.response.use( ...@@ -80,8 +82,11 @@ apiClient.interceptors.response.use(
isRefreshing = true; isRefreshing = true;
try { try {
// Try refreshing the token (cookies will be sent automatically) const storedRefreshToken = useAuthStore.getState().refreshToken || localStorage.getItem("refreshToken");
const response = await refreshClient.post("/auth/refresh", {}); // 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) { if (response.data?.success) {
const newData = response.data?.data; const newData = response.data?.data;
......
...@@ -74,7 +74,11 @@ const LoginPage: React.FC = () => { ...@@ -74,7 +74,11 @@ const LoginPage: React.FC = () => {
}); });
if (response.success && response.data) { if (response.success && response.data) {
updateRememberedEmail(values.email, values.rememberMe); updateRememberedEmail(values.email, values.rememberMe);
setAuth(response.data.user, "", ""); setAuth(
response.data.user,
response.data.accessToken || "",
response.data.refreshToken || ""
);
openSnackbar({ openSnackbar({
type: "success", type: "success",
text: t("auth.login.success"), text: t("auth.login.success"),
......
...@@ -12,7 +12,7 @@ export const authService = { ...@@ -12,7 +12,7 @@ export const authService = {
return response.data; 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); const response = await apiClient.post("/auth/login", data);
return response.data; return response.data;
}, },
......
...@@ -9,7 +9,7 @@ import { ...@@ -9,7 +9,7 @@ import {
function ensureSuccess<T extends { success: boolean; message?: string }>(response: T): T { function ensureSuccess<T extends { success: boolean; message?: string }>(response: T): T {
if (!response.success) { 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; return response;
} }
......
...@@ -10,7 +10,7 @@ import { ...@@ -10,7 +10,7 @@ import {
function ensureSuccess<T extends { success: boolean; message?: string }>(response: T): T { function ensureSuccess<T extends { success: boolean; message?: string }>(response: T): T {
if (!response.success) { 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; 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