From 9d2027770669292c94f9ca518fdd724a0bb8520b Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 31 May 2022 17:07:25 +0200 Subject: [PATCH] Globally configure SWR --- pages/_app.tsx | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/pages/_app.tsx b/pages/_app.tsx index 75ffb8d..0ad1eaf 100644 --- a/pages/_app.tsx +++ b/pages/_app.tsx @@ -9,18 +9,36 @@ import { StaticImageData } from 'next/image' import { appWithTranslation } from 'next-i18next' import { FestaLoginData } from '../types/user' import {useStoredLogin} from "../hooks/useStoredLogin" +import { Fetcher, SWRConfig } from 'swr' +import axios, { AxiosRequestConfig } from 'axios' const App = ({ Component, pageProps }: AppProps): JSX.Element => { - const [login, setLogin] = useState(null) const [postcard, setPostcard] = useState(defaultPostcard) + + const [login, setLogin] = useState(null) useStoredLogin(setLogin) + const axiosConfig = { + headers: { + "Authorization": login ? `Bearer ${login.token}` : "", + } + } + + const swrConfig = { + fetcher: async (resource: string, localAxiosConfig: AxiosRequestConfig) => { + const response = await axios.get(resource, {...axiosConfig, ...localAxiosConfig}) + return response.data + } + } + return ( + + )