diff --git a/src/hooks/queries.ts b/src/hooks/queries.ts index a1f3da8..f3363ad 100644 --- a/src/hooks/queries.ts +++ b/src/hooks/queries.ts @@ -183,10 +183,10 @@ function useMoreBooksAuthors(id: string | undefined) { }); } -function useBook(pathUrl: string | undefined) { +function useBook(pathUrl: string | undefined, token?: string | null) { return useSuspenseQuery({ queryKey: [keys.one, pathUrl], - queryFn: () => getBook(pathUrl), + queryFn: () => getBook(pathUrl, token), refetchOnWindowFocus: false, gcTime: 3000, retry: 1, diff --git a/src/pages/Book.tsx b/src/pages/Book.tsx index 4151992..a554e60 100644 --- a/src/pages/Book.tsx +++ b/src/pages/Book.tsx @@ -49,6 +49,7 @@ const MoreBooks = lazy(() => import('@components/cards/MoreBooks')); export default function Book() { const shareUrl = window.location.href; + const getToken = window.localStorage.getItem('app_tk'); const { pathUrl } = useParams(); const { currentUser } = useAuth(); const grayColor = useColorModeValue('gray.200', 'gray.600'); @@ -80,7 +81,7 @@ export default function Book() { let uiLink; let btnMoreOptions; - const { data } = useBook(pathUrl); + const { data } = useBook(pathUrl, getToken); const { mutate, isSuccess, isPending } = useDeleteBook(); const isCurrentUserAuthor = currentUser && currentUser.uid === data.userId; diff --git a/src/services/api.ts b/src/services/api.ts index 375cd09..07e45f5 100644 --- a/src/services/api.ts +++ b/src/services/api.ts @@ -13,8 +13,18 @@ async function getBooksPaginate(page: number | undefined) { return await fetchData(`${API_URL}/books?limit=10&page=${page}`); } -async function getBook(pathUrl: string | undefined) { - return await fetchData(`${API_URL}/books/path/${pathUrl}`); +async function getBook(pathUrl: string | undefined, token?: string | null) { + const headers = new Headers(); + headers.append('content-type', 'application/json'); + + if (token) { + headers.append('Authorization', `Bearer ${token}`); + } + + return await fetchData(`${API_URL}/books/path/${pathUrl}`, { + method: 'GET', + headers, + }); } async function getBooksFilterPaginated(