Skip to content

Commit f39867a

Browse files
fix(next-auth): always check typeof BroadcastChannel (#12937)
* fix: safely call postMessage * fix: change logic * fix: satisfies BroadcastChannel --------- Co-authored-by: Thang Vu <[email protected]>
1 parent 24b82d9 commit f39867a

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

packages/next-auth/src/react.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,27 @@ export const __NEXTAUTH: AuthClientConfig = {
6767
_getSession: () => {},
6868
}
6969

70+
// https://github.com/nextauthjs/next-auth/pull/10762
7071
let broadcastChannel: BroadcastChannel | null = null
7172

7273
function getNewBroadcastChannel() {
73-
return new BroadcastChannel("next-auth")
74-
}
75-
76-
function broadcast() {
7774
if (typeof BroadcastChannel === "undefined") {
7875
return {
7976
postMessage: () => {},
8077
addEventListener: () => {},
8178
removeEventListener: () => {},
82-
}
79+
name: "next-auth",
80+
onmessage: null,
81+
onmessageerror: null,
82+
close: () => {},
83+
dispatchEvent: () => false,
84+
} satisfies BroadcastChannel
8385
}
8486

87+
return new BroadcastChannel("next-auth")
88+
}
89+
90+
function broadcast() {
8591
if (broadcastChannel === null) {
8692
broadcastChannel = getNewBroadcastChannel()
8793
}
@@ -180,8 +186,8 @@ export async function getSession(params?: GetSessionParams) {
180186
params
181187
)
182188
if (params?.broadcast ?? true) {
183-
const broadcastChannel = getNewBroadcastChannel()
184-
broadcastChannel.postMessage({
189+
// https://github.com/nextauthjs/next-auth/pull/11470
190+
getNewBroadcastChannel().postMessage({
185191
event: "session",
186192
data: { trigger: "getSession" },
187193
})

0 commit comments

Comments
 (0)