Skip to content

[next-auth][v5/beta][react] Fix #10244: Client sends multiple GET /session requests #13084

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
24 changes: 20 additions & 4 deletions packages/next-auth/src/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,29 @@ export interface GetSessionParams {
broadcast?: boolean
}

export async function getSession(params?: GetSessionParams) {
const session = await fetchData<Session>(
async function _fetchSession(params?: GetSessionParams) {
return fetchData<Session>(
"session",
__NEXTAUTH,
logger,
params
)
}

// https://github.com/nextauthjs/next-auth/issues/10244#issuecomment-3029947368
async function _getSession(params?: GetSessionParams) {
const session = await _fetchSession(params)
if (params?.broadcast ?? true) {
broadcast().postMessage({
event: "session",
data: { trigger: "getSession" },
})
}
return session
}

export async function getSession(params?: GetSessionParams) {
const session = await _fetchSession(params)
if (params?.broadcast ?? true) {
// https://github.com/nextauthjs/next-auth/pull/11470
getNewBroadcastChannel().postMessage({
Expand Down Expand Up @@ -413,7 +429,7 @@ export function SessionProvider(props: SessionProviderProps) {
// or if there are events from other tabs/windows
if (storageEvent || __NEXTAUTH._session === undefined) {
__NEXTAUTH._lastSync = now()
__NEXTAUTH._session = await getSession({
__NEXTAUTH._session = await _getSession({
broadcast: !storageEvent,
})
setSession(__NEXTAUTH._session)
Expand All @@ -438,7 +454,7 @@ export function SessionProvider(props: SessionProviderProps) {

// An event or session staleness occurred, update the client session.
__NEXTAUTH._lastSync = now()
__NEXTAUTH._session = await getSession()
__NEXTAUTH._session = await _getSession()
setSession(__NEXTAUTH._session)
} catch (error) {
logger.error(
Expand Down