feat: Add JWT authentication to backend, configure frontend API client, fix streak layout
This commit is contained in:
27
src/lib/apiClient.ts
Normal file
27
src/lib/apiClient.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
export async function fetchWithAuth(url: string, options: RequestInit = {}): Promise<Response> {
|
||||
const isServer = typeof window === 'undefined';
|
||||
|
||||
if (isServer) {
|
||||
// If we're on the server-side, we should pass along headers from next/headers
|
||||
// or just let it fall back to standard fetch if no auth is needed for server-side fetches.
|
||||
// For this app, most data fetching seems to be client-side.
|
||||
return fetch(url, options);
|
||||
}
|
||||
|
||||
// Get token from localStorage
|
||||
const token = localStorage.getItem('active_session_token');
|
||||
|
||||
// Prepare headers
|
||||
const headers = new Headers(options.headers || {});
|
||||
|
||||
// Apply token if it exists
|
||||
if (token) {
|
||||
headers.set('Authorization', `Bearer ${token}`);
|
||||
}
|
||||
|
||||
// Perform fetch with updated headers
|
||||
return fetch(url, {
|
||||
...options,
|
||||
headers,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user