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

const stytch = new StytchClient('${publicToken}');

const unsubscribe = stytch.session.onChange((session) => {
  if (session) {
    console.log('Session found:', session.session_id);
  } else {
    console.log('No active session');
  }
});

window.addEventListener('beforeunload', () => {
  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 Session change event.