fix: force-dynamic pa write-sidan + saker useSession i useAuthFetch
This commit is contained in:
@@ -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 (
|
||||||
<>
|
<>
|
||||||
|
|||||||
@@ -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],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user