refactor: useAuthFetch-hook för automatisk JWT-header i klientanrop

This commit is contained in:
Nils-Johan Gynther
2026-04-19 21:48:13 +02:00
parent 92d0b066f1
commit b4d9e3dd5f
4 changed files with 40 additions and 17 deletions
@@ -2,7 +2,7 @@
import { useState, useEffect } from 'react';
import { useRouter } from 'next/navigation';
import { useSession } from 'next-auth/react';
import { useAuthFetch } from '../../../lib/use-auth-fetch';
import { fetchJson } from '../../../lib/api';
import { parseErrorResponse } from '../../../lib/error-handler';
import type { Product } from '../../../features/inventory/types';
@@ -37,7 +37,7 @@ type Step = 'input' | 'review' | 'saving' | 'saved';
export default function WriteRecipePage() {
const router = useRouter();
const { data: session } = useSession();
const authFetch = useAuthFetch();
const [step, setStep] = useState<Step>('input');
const [markdown, setMarkdown] = useState('');
const [parsed, setParsed] = useState<ParseResult | null>(null);
@@ -193,9 +193,8 @@ export default function WriteRecipePage() {
};
try {
const res = await fetch('/api/recipes', {
const res = await authFetch('/api/recipes', {
method: 'POST',
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${session?.accessToken}` },
body: JSON.stringify(body),
});