fix: force-dynamic pa write-sidan + saker useSession i useAuthFetch
This commit is contained in:
@@ -13,17 +13,20 @@ import { useCallback } from 'react';
|
||||
* const res = await authFetch('/api/recipes/1', { method: 'PATCH', body: JSON.stringify(data) });
|
||||
*/
|
||||
export function useAuthFetch() {
|
||||
const { data: session } = useSession();
|
||||
const sessionResult = useSession();
|
||||
const accessToken = sessionResult?.data?.accessToken ?? '';
|
||||
|
||||
return useCallback(
|
||||
(url: string, init: RequestInit = {}): Promise<Response> => {
|
||||
const headers = new Headers(init.headers);
|
||||
headers.set('Authorization', `Bearer ${session?.accessToken ?? ''}`);
|
||||
if (accessToken) {
|
||||
headers.set('Authorization', `Bearer ${accessToken}`);
|
||||
}
|
||||
if (!headers.has('Content-Type') && init.body && typeof init.body === 'string') {
|
||||
headers.set('Content-Type', 'application/json');
|
||||
}
|
||||
return fetch(url, { ...init, headers });
|
||||
},
|
||||
[session?.accessToken],
|
||||
[accessToken],
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user