Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add ability to customize client-side fetch calls #12204

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/next-auth/src/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) */
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -153,7 +158,9 @@ export async function fetchData<T = any>(
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 } : {}),
},
Expand Down
14 changes: 13 additions & 1 deletion packages/next-auth/src/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: () => {},
Expand Down Expand Up @@ -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",
},
Expand Down Expand Up @@ -323,8 +326,10 @@ export async function signOut<R extends boolean = true>(
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",
},
Expand Down Expand Up @@ -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,
Expand Down
Loading