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 member changes
const unsubscribe = stytch.self.onChange((member) => {
  if (member) {
    console.log('Member changed:', member.name);
  } else {
    console.log('No logged in member found');
  }
});

// Cleanup subscription when done
window.addEventListener('beforeunload', () => {
  unsubscribe();
});
{
  "organization_id": "organization-test-07971b06-ac8b-4cdb-9c15-63b17e653931",
  "member_id": "member-test-32fc5024-9c09-4da3-bd2e-c9ce4da9375f",
  "email_address": "sandbox@stytch.com",
  "email_address_verified": true,
  "retired_email_addresses": ["old@stytch.com"],
  "status": "active",
  "name": "Test User",
  "is_breakglass": false,
  "member_password_id": "member-password-test-32fc5024-9c09-4da3-bd2e-c9ce4da9375f",
  "mfa_enrolled": false,
  "mfa_phone_number": null,
  "mfa_phone_number_verified": false,
  "default_mfa_method": "sms_otp",
  "totp_registration_id": "totp-registration-test-32fc5024-9c09-4da3-bd2e-c9ce4da9375f",
  "oauth_registrations": [],
  "sso_registrations": [],
  "roles": [],
  "trusted_metadata": {},
  "untrusted_metadata": {},
  "created_at": "2023-01-01T00:00:00Z",
  "updated_at": "2023-01-01T00:00:00Z"
}
The Stytch SDK stores the currently logged-in in local storage. and will periodically refresh the cached data from the API. Use the self.onChange method to listen for and react to changes to the Member object.
Before using this method, enable in the Frontend SDK page.
If you need to asynchronously fetch guaranteed-fresh data from the API, use the self.get method.

Parameters

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

Return value

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