Update project 4 frontend start
This commit is contained in:
18
frontend-client/src/api/client.ts
Normal file
18
frontend-client/src/api/client.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import axios from "axios";
|
||||
import { useAuthStore } from "@/store/authStore";
|
||||
|
||||
const apiClient = axios.create({
|
||||
baseURL: process.env.NEXT_PUBLIC_API_URL ?? "http://localhost:8081/api",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
|
||||
apiClient.interceptors.request.use((config) => {
|
||||
// getState() — безопасен вне React-дерева (server actions, route handlers тоже работают)
|
||||
const token = useAuthStore.getState().token;
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
return config;
|
||||
});
|
||||
|
||||
export default apiClient;
|
||||
Reference in New Issue
Block a user