You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Title: Synchronization Issue Between next-auth Session and useSWR Fetching
Description:
I’m using next-auth with useSWR for data fetching, and I'm trying to set up an Axios interceptor to add an Authorization token to the request headers. However, I’m encountering a synchronization issue where useSWR is firing before I reset the axios interceptor.
Steps to Reproduce:
Use useSWR to fetch data with Axios.
In the Axios request interceptor, set the Authorization header with the token from next-auth's useSession().
useSWR is called before the axios interceptor is reset.
Code Example:
Global setup
// src/providers/session-provider.ts
"use client";
import { ReactNode, useEffect } from "react";
import { useSession } from "next-auth/react";
import { setAxiosInteceptors } from "@/utils/fetcher";
import { useIsomorphicLayoutEffect } from "ahooks";
export default function SessionProvider({ children }: { children: ReactNode }) {
const { data: session } = useSession();
useIsomorphicLayoutEffect(() => {
if (session) {
setAxiosInteceptors(session);
}
}, [session]);
return children;
}
// src/app/[locale]/layout.tsx
import SessionProvider from "@/providers/session-provider";
<AuthProvider session={session}>
<SessionProvider>
<NextIntlClientProvider messages={messages}>
{children}
</NextIntlClientProvider>
</SessionProvider>
</AuthProvider>
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Title: Synchronization Issue Between
next-auth
Session anduseSWR
FetchingDescription:
I’m using
next-auth
withuseSWR
for data fetching, and I'm trying to set up an Axios interceptor to add an Authorization token to the request headers. However, I’m encountering a synchronization issue whereuseSWR
is firing before I reset the axios interceptor.Steps to Reproduce:
useSWR
to fetch data with Axios.Authorization
header with the token fromnext-auth
'suseSession()
.useSWR
is called before the axios interceptor is reset.Code Example:
Global setup
My fetcher
How the fetcher is used
Expected Behavior:
when useSWR calls the fetcher, axios instance interceptor should already be synchronized with useSession.
Environment:
Beta Was this translation helpful? Give feedback.
All reactions