fix: force-dynamic pa write-sidan + saker useSession i useAuthFetch

This commit is contained in:
Nils-Johan Gynther
2026-04-19 21:50:41 +02:00
parent 8ab807aba2
commit 0f71d2569b
2 changed files with 8 additions and 3 deletions
+2
View File
@@ -1,6 +1,8 @@
import Navigation from '../../Navigation'; import Navigation from '../../Navigation';
import WriteRecipePage from './WriteRecipePage'; import WriteRecipePage from './WriteRecipePage';
export const dynamic = 'force-dynamic';
export default function Page() { export default function Page() {
return ( return (
<> <>
+6 -3
View File
@@ -13,17 +13,20 @@ import { useCallback } from 'react';
* const res = await authFetch('/api/recipes/1', { method: 'PATCH', body: JSON.stringify(data) }); * const res = await authFetch('/api/recipes/1', { method: 'PATCH', body: JSON.stringify(data) });
*/ */
export function useAuthFetch() { export function useAuthFetch() {
const { data: session } = useSession(); const sessionResult = useSession();
const accessToken = sessionResult?.data?.accessToken ?? '';
return useCallback( return useCallback(
(url: string, init: RequestInit = {}): Promise<Response> => { (url: string, init: RequestInit = {}): Promise<Response> => {
const headers = new Headers(init.headers); 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') { if (!headers.has('Content-Type') && init.body && typeof init.body === 'string') {
headers.set('Content-Type', 'application/json'); headers.set('Content-Type', 'application/json');
} }
return fetch(url, { ...init, headers }); return fetch(url, { ...init, headers });
}, },
[session?.accessToken], [accessToken],
); );
} }