diff --git a/packages/next-auth/src/lib/client.ts b/packages/next-auth/src/lib/client.ts index e702ac70e1..516e437bea 100644 --- a/packages/next-auth/src/lib/client.ts +++ b/packages/next-auth/src/lib/client.ts @@ -16,6 +16,7 @@ export interface AuthClientConfig { basePath: string baseUrlServer: string basePathServer: string + fetchOptions: RequestInit /** Stores last session response */ _session?: Session | null | undefined /** Used for timestamp since last sycned (in seconds) */ @@ -116,6 +117,10 @@ export interface SessionProviderProps { session?: Session | null baseUrl?: string basePath?: string + /** + * Allows for customizing underlying fetch calls made to next-auth APIs + */ + fetchOptions: RequestInit /** * A time interval (in seconds) after which the session will be re-fetched. * If set to `0` (default), the session is not polled. @@ -153,7 +158,9 @@ export async function fetchData( const url = `${apiBaseUrl(__NEXTAUTH)}/${path}` try { const options: RequestInit = { + ...__NEXTAUTH.fetchOptions, headers: { + ...(__NEXTAUTH.fetchOptions.headers || {}), "Content-Type": "application/json", ...(req?.headers?.cookie ? { cookie: req.headers.cookie } : {}), }, diff --git a/packages/next-auth/src/react.tsx b/packages/next-auth/src/react.tsx index e23e3609e0..0e22b684df 100644 --- a/packages/next-auth/src/react.tsx +++ b/packages/next-auth/src/react.tsx @@ -67,6 +67,7 @@ export const __NEXTAUTH: AuthClientConfig = { basePathServer: parseUrl( process.env.NEXTAUTH_URL_INTERNAL ?? process.env.NEXTAUTH_URL ).path, + fetchOptions: {}, _lastSync: 0, _session: undefined, _getSession: () => {}, @@ -270,8 +271,10 @@ export async function signIn< const res = await fetch( `${signInUrl}?${new URLSearchParams(authorizationParams)}`, { + ...__NEXTAUTH.fetchOptions, method: "post", headers: { + ...(__NEXTAUTH.fetchOptions.headers || {}), "Content-Type": "application/x-www-form-urlencoded", "X-Auth-Return-Redirect": "1", }, @@ -323,8 +326,10 @@ export async function signOut( const baseUrl = apiBaseUrl(__NEXTAUTH) const csrfToken = await getCsrfToken() const res = await fetch(`${baseUrl}/signout`, { + ...__NEXTAUTH.fetchOptions, method: "post", headers: { + ...(__NEXTAUTH.fetchOptions.headers || {}), "Content-Type": "application/x-www-form-urlencoded", "X-Auth-Return-Redirect": "1", }, @@ -363,9 +368,16 @@ export function SessionProvider(props: SessionProviderProps) { throw new Error("React Context is unavailable in Server Components") } - const { children, basePath, refetchInterval, refetchWhenOffline } = props + const { + children, + basePath, + fetchOptions, + refetchInterval, + refetchWhenOffline, + } = props if (basePath) __NEXTAUTH.basePath = basePath + if (fetchOptions) __NEXTAUTH.fetchOptions = fetchOptions /** * If session was `null`, there was an attempt to fetch it,