Add basic authentication
This commit is contained in:
@@ -5,3 +5,20 @@ export const makeApiUrl = (endpoint: string) => {
|
||||
const path = endpoint.startsWith('/') ? endpoint : `/${endpoint}`;
|
||||
return `${base}${path}`;
|
||||
};
|
||||
|
||||
export const authHeaders = () => {
|
||||
try {
|
||||
const token = localStorage.getItem('token');
|
||||
return token ? { Authorization: `Bearer ${token}` } : {};
|
||||
} catch (e) {
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
export const fetchWithAuth = (endpoint: string, options: RequestInit = {}) => {
|
||||
const headers = {
|
||||
...(options.headers || {}),
|
||||
...authHeaders(),
|
||||
} as Record<string, string>;
|
||||
return fetch(makeApiUrl(endpoint), { ...options, headers });
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user