Skip to main content
import { StytchB2BClient } from '@stytch/vanilla-js/b2b';

const stytch = new StytchB2BClient('public-token-test-b8c84de4-7d58-4ffc-9341-432b56596862');

// Subscribe to session changes
const unsubscribe = stytch.session.onChange((session) => {
  if (session) {
    console.log('Session ID:', session.member_session_id);
    // Update UI with session data
    document.getElementById('session-status').textContent =
      `Session ID: ${session.member_session_id}`;
  } else {
    console.log('No active session');
    document.getElementById('session-status').textContent = 'No active session';
  }
});

// Later, when you want to unsubscribe
// unsubscribe();
The Stytch SDK caches the Session of the logged-in . Use the session.onChange method to listen for and react to changes to the Session object.

Parameters

callback
function
The callback function to call when the session changes. The first parameter is the updated Session object.

Return value

unsubscribe
function
The function to call to unsubscribe from the Organization change event.