fix: dashboard API client sends JWT auth token
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
import { getToken, removeToken } from './auth';
|
||||||
|
|
||||||
const API_URL = process.env.NEXT_PUBLIC_API_URL || '';
|
const API_URL = process.env.NEXT_PUBLIC_API_URL || '';
|
||||||
|
|
||||||
class ApiClient {
|
class ApiClient {
|
||||||
@@ -7,6 +9,31 @@ class ApiClient {
|
|||||||
this.baseUrl = baseUrl;
|
this.baseUrl = baseUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private getHeaders(): Record<string, string> {
|
||||||
|
const headers: Record<string, string> = {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
};
|
||||||
|
const token = typeof window !== 'undefined' ? getToken() : null;
|
||||||
|
if (token) {
|
||||||
|
headers['Authorization'] = `Bearer ${token}`;
|
||||||
|
}
|
||||||
|
return headers;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async handleResponse(response: Response): Promise<any> {
|
||||||
|
if (response.status === 401) {
|
||||||
|
removeToken();
|
||||||
|
if (typeof window !== 'undefined') {
|
||||||
|
window.location.href = '/login';
|
||||||
|
}
|
||||||
|
throw new Error('Unauthorized');
|
||||||
|
}
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`API error: ${response.status}`);
|
||||||
|
}
|
||||||
|
return response.json();
|
||||||
|
}
|
||||||
|
|
||||||
async get(path: string, params?: Record<string, any>): Promise<any> {
|
async get(path: string, params?: Record<string, any>): Promise<any> {
|
||||||
const url = new URL(path, this.baseUrl || window.location.origin);
|
const url = new URL(path, this.baseUrl || window.location.origin);
|
||||||
if (params) {
|
if (params) {
|
||||||
@@ -16,71 +43,37 @@ class ApiClient {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
const response = await fetch(url.toString(), { headers: this.getHeaders() });
|
||||||
const response = await fetch(url.toString(), {
|
return this.handleResponse(response);
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`API error: ${response.status}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return response.json();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async post(path: string, data: any): Promise<any> {
|
async post(path: string, data: any): Promise<any> {
|
||||||
const url = new URL(path, this.baseUrl || window.location.origin);
|
const url = new URL(path, this.baseUrl || window.location.origin);
|
||||||
|
|
||||||
const response = await fetch(url.toString(), {
|
const response = await fetch(url.toString(), {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: this.getHeaders(),
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
body: JSON.stringify(data),
|
body: JSON.stringify(data),
|
||||||
});
|
});
|
||||||
|
return this.handleResponse(response);
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`API error: ${response.status}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return response.json();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async put(path: string, data?: any): Promise<any> {
|
async put(path: string, data?: any): Promise<any> {
|
||||||
const url = new URL(path, this.baseUrl || window.location.origin);
|
const url = new URL(path, this.baseUrl || window.location.origin);
|
||||||
|
|
||||||
const response = await fetch(url.toString(), {
|
const response = await fetch(url.toString(), {
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
headers: {
|
headers: this.getHeaders(),
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
body: data ? JSON.stringify(data) : undefined,
|
body: data ? JSON.stringify(data) : undefined,
|
||||||
});
|
});
|
||||||
|
return this.handleResponse(response);
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`API error: ${response.status}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return response.json();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async delete(path: string): Promise<any> {
|
async delete(path: string): Promise<any> {
|
||||||
const url = new URL(path, this.baseUrl || window.location.origin);
|
const url = new URL(path, this.baseUrl || window.location.origin);
|
||||||
|
|
||||||
const response = await fetch(url.toString(), {
|
const response = await fetch(url.toString(), {
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
headers: {
|
headers: this.getHeaders(),
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
return this.handleResponse(response);
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`API error: ${response.status}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return response.json();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user