Skip to content

feat: export broadcast function from react module#13374

Open
chentsulin wants to merge 1 commit intonextauthjs:mainfrom
chentsulin:feat/export-broadcast-function
Open

feat: export broadcast function from react module#13374
chentsulin wants to merge 1 commit intonextauthjs:mainfrom
chentsulin:feat/export-broadcast-function

Conversation

@chentsulin
Copy link

Exporting the broadcast function to allow external usage of the BroadcastChannel functionality.

☕️ Reasoning

This gives us the opportunity to directly listen to the broadcasted signout event to achieve reload on other tabs, while the tab that executes signOut won't receive the event itself.

'use client';

import { broadcast, useSession } from 'next-auth/react';
import { useEffect } from 'react';

interface NextAuthBroadcastMessage {
  event: string;
  data?: {
    trigger?: string;
  };
}

export function SignOutListener() {
  const { data: session } = useSession();

  useEffect(() => {
    const channel = broadcast();

    const handler = (message: MessageEvent<NextAuthBroadcastMessage>) => {
      const { event, data } = message.data;

      // message format: { event: "session", data: { trigger: "signout" } }
      if (session && event === 'session' && data?.trigger === 'signout') {
        window.location.reload();
      }
    };

    channel.addEventListener('message', handler);

    return () => {
      channel.removeEventListener('message', handler);
    };
  }, [session]);

  return null;
}

🧢 Checklist

  • Ready to be merged

Exporting the broadcast function to allow external usage of the BroadcastChannel functionality.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@vercel
Copy link

vercel bot commented Feb 4, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
auth-docs Ready Ready Preview, Comment Feb 4, 2026 4:29am
1 Skipped Deployment
Project Deployment Actions Updated (UTC)
next-auth-docs Ignored Ignored Preview Feb 4, 2026 4:29am

Request Review

@vercel
Copy link

vercel bot commented Feb 4, 2026

@chentsulin is attempting to deploy a commit to the authjs Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant