> ## Documentation Index
> Fetch the complete documentation index at: https://stytch.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# On Change Session

> Listen for changes to the current Session using the Stytch Next.js SDK

export const organization = "Represents an instance or tenant in your application, typically mapping to each of your top-level customers.";

export const member = "Represents an individual end user's account within a given Organization, uniquely identified within that Organization by their email address.";

export const isReact_0 = true

{isReact_0 &&
<Info>
  In React, use the <a href="../../hooks/use-stytch-member-session"><code>useStytchMemberSession</code></a> hook to easily access the current session and react to changes.
</Info>
}

The Stytch SDK caches the Session of the logged-in <Tooltip tip={member}>Member</Tooltip>. Use the `session.onChange` method to listen for and react to changes to the Session object.

## Parameters

<ResponseField name="callback" type="function">
  The callback function to call when the session changes.  The first parameter is the updated Session object.

  <Expandable title="parameters">
    <ResponseField name="session" type="object">
      The updated Session object.

      <Expandable title="properties">
        <ResponseField name="member_session_id" type="string">
          Globally unique UUID that identifies the Session.
        </ResponseField>

        <ResponseField name="member_id" type="string">
          Globally unique UUID that identifies a specific Member.
        </ResponseField>

        <ResponseField name="authentication_factors" type="array[objects]">
          An array of authentication factors that comprise a Session.

          <Expandable title="properties">
            <ResponseField name="type" type="string">
              The type of authentication factor. The possible values are: `email_otp`, `impersonated`, `imported`, `magic_link`, `oauth`, `otp`, `password`, `recovery_codes`, `sso`, `trusted_auth_token`, or `totp`.
            </ResponseField>

            <ResponseField name="delivery_method" type="string">
              The method that was used to deliver the authentication factor. The possible values depend on the type:

              * `email_otp`: Only `email`.
              * `impersonated`: Only `impersonation`.
              * `imported`:  Only `imported_auth0`.
              * `magic_link`: Only `email`.
              * `oauth`: `oauth_google`, `oauth_microsoft`, `oauth_hubspot`, `oauth_slack`, or `oauth_github`.
                You may see an 'exchange' delivery method when a non-email-verifying OAuth factor originally authenticated in one organization is exchanged for a factor in another organization. This can happen during authentication flows such as [session exchange](/api-reference/b2b/api/sessions/exchange-session). The non-email-verifying OAuth providers are Hubspot, Slack, and Github. Google is also considered non-email-verifying when the HD claim is empty. The possible exchange values are `oauth_exchange_google`, `oauth_exchange_hubspot`, `oauth_exchange_slack`, or `oauth_exchange_github`. The final possible value is `oauth_access_token_exchange`, if this factor came from an [access token exchange flow](/api-reference/b2b/api/sessions/exchange-access-token).
                * `otp`: Only `sms`.
                * `password`: Only `knowledge`.
                * `recovery_codes`: Only `recovery_code`.
                * `sso`: `sso_saml` or `sso_oidc`.
                * `trusted_auth_token`: Only `trusted_token_exchange`.
                * `totp`: Only `authenticator_app`.
            </ResponseField>

            <ResponseField name="created_at" type="string">
              The timestamp when the factor was initially authenticated.
            </ResponseField>

            <ResponseField name="last_authenticated_at" type="string">
              The timestamp when the factor was last authenticated.
            </ResponseField>

            <ResponseField name="updated_at" type="string">
              The timestamp when the factor was last updated.
            </ResponseField>

            <ResponseField name="sequence_order" type="string">
              Either `PRIMARY` or `SECONDARY`. Secondary factor types include `otp`, `totp`, and `recovery_codes`. All other factors are primary.
            </ResponseField>

            <ResponseField name="email_factor" type="object">
              Information about the email factor, if one is present.

              <Expandable title="properties">
                <ResponseField name="email_address" type="string">
                  The email address of the Member.
                </ResponseField>

                <ResponseField name="email_id" type="string">
                  The globally unique UUID of the Member's email.
                </ResponseField>
              </Expandable>
            </ResponseField>

            <ResponseField name="phone_number_factor" type="object">
              Information about the phone number factor, if one is present.

              <Expandable title="properties">
                <ResponseField name="phone_number" type="string">
                  The phone number.
                </ResponseField>

                <ResponseField name="phone_id" type="string">
                  The globally unique UUID of the phone number.
                </ResponseField>
              </Expandable>
            </ResponseField>

            <ResponseField name="google_oauth_factor" type="object">
              Information about the Google OAuth factor, if one is present.

              <Expandable title="properties">
                <ResponseField name="id" type="string">
                  The unique ID of the OAuth registration.
                </ResponseField>

                <ResponseField name="email_id" type="string">
                  The globally unique UUID of the Member's email.
                </ResponseField>

                <ResponseField name="provider_subject" type="string">
                  The unique identifier for the User within the OAuth provider. Also commonly called the sub or "Subject field" in OAuth protocols.
                </ResponseField>
              </Expandable>
            </ResponseField>

            <ResponseField name="microsoft_oauth_factor" type="object">
              Information about the Microsoft OAuth factor, if one is present.

              <Expandable title="properties">
                <ResponseField name="id" type="string">
                  The unique ID of the OAuth registration.
                </ResponseField>

                <ResponseField name="email_id" type="string">
                  The globally unique UUID of the Member's email.
                </ResponseField>

                <ResponseField name="provider_subject" type="string">
                  The unique identifier for the User within the OAuth provider. Also commonly called the sub or "Subject field" in OAuth protocols.
                </ResponseField>
              </Expandable>
            </ResponseField>

            <ResponseField name="hubspot_oauth_factor" type="object">
              Information about the Hubspot OAuth factor, if one is present.

              <Expandable title="properties">
                <ResponseField name="id" type="string">
                  The unique ID of the OAuth registration.
                </ResponseField>

                <ResponseField name="email_id" type="string">
                  The globally unique UUID of the Member's email.
                </ResponseField>

                <ResponseField name="provider_subject" type="string">
                  The unique identifier for the User within the OAuth provider. Also commonly called the sub or "Subject field" in OAuth protocols.
                </ResponseField>
              </Expandable>
            </ResponseField>

            <ResponseField name="github_oauth_factor" type="object">
              Information about the Github OAuth factor, if one is present.

              <Expandable title="properties">
                <ResponseField name="id" type="string">
                  The unique ID of the OAuth registration.
                </ResponseField>

                <ResponseField name="email_id" type="string">
                  The globally unique UUID of the Member's email.
                </ResponseField>

                <ResponseField name="provider_subject" type="string">
                  The unique identifier for the User within the OAuth provider. Also commonly called the sub or "Subject field" in OAuth protocols.
                </ResponseField>
              </Expandable>
            </ResponseField>

            <ResponseField name="slack_oauth_factor" type="object">
              Information about the Slack OAuth factor, if one is present.

              <Expandable title="properties">
                <ResponseField name="id" type="string">
                  The unique ID of the OAuth registration.
                </ResponseField>

                <ResponseField name="email_id" type="string">
                  The globally unique UUID of the Member's email.
                </ResponseField>

                <ResponseField name="provider_subject" type="string">
                  The unique identifier for the User within the OAuth provider. Also commonly called the sub or "Subject field" in OAuth protocols.
                </ResponseField>
              </Expandable>
            </ResponseField>

            <ResponseField name="google_oauth_exchange_factor" type="object">
              Information about the Google OAuth Exchange factor, if one is present.

              <Expandable title="properties">
                <ResponseField name="email_id" type="string">
                  The globally unique UUID of the email address.
                </ResponseField>
              </Expandable>
            </ResponseField>

            <ResponseField name="hubspot_oauth_exchange_factor" type="object">
              Information about the Hubspot OAuth Exchange factor, if one is present.

              <Expandable title="properties">
                <ResponseField name="email_id" type="string">
                  The globally unique UUID of the email address.
                </ResponseField>
              </Expandable>
            </ResponseField>

            <ResponseField name="github_oauth_exchange_factor" type="object">
              Information about the Github OAuth Exchange factor, if one is present.

              <Expandable title="properties">
                <ResponseField name="email_id" type="string">
                  The globally unique UUID of the email address.
                </ResponseField>
              </Expandable>
            </ResponseField>

            <ResponseField name="slack_oauth_exchange_factor" type="object">
              Information about the Slack OAuth Exchange factor, if one is present.

              <Expandable title="properties">
                <ResponseField name="email_id" type="string">
                  The globally unique UUID of the email address.
                </ResponseField>
              </Expandable>
            </ResponseField>

            <ResponseField name="saml_sso_factor" type="object">
              Information about the SAML SSO factor, if one is present.

              <Expandable title="properties">
                <ResponseField name="id" type="string">
                  The unique ID of an SSO Registration.
                </ResponseField>

                <ResponseField name="provider_id" type="string">
                  Globally unique UUID that identifies a specific SAML Connection.
                </ResponseField>

                <ResponseField name="external_id" type="string">
                  The ID of the member given by the identity provider.
                </ResponseField>
              </Expandable>
            </ResponseField>

            <ResponseField name="oidc_sso_factor" type="object">
              Information about the OIDC SSO factor, if one is present.

              <Expandable title="properties">
                <ResponseField name="id" type="string">
                  The unique ID of an SSO Registration.
                </ResponseField>

                <ResponseField name="provider_id" type="string">
                  Globally unique UUID that identifies a specific OIDC Connection.
                </ResponseField>

                <ResponseField name="external_id" type="string">
                  The ID of the member given by the identity provider.
                </ResponseField>
              </Expandable>
            </ResponseField>

            <ResponseField name="authenticator_app_factor" type="object">
              Information about the TOTP-backed Authenticator App factor, if one is present.

              <Expandable title="properties">
                <ResponseField name="totp_id" type="string">
                  Globally unique UUID that identifies the TOTP instance.
                </ResponseField>
              </Expandable>
            </ResponseField>

            <ResponseField name="impersonated_factor" type="object">
              Information about the impersonated factor, if one is present.

              <Expandable title="properties">
                <ResponseField name="impersonator_id" type="string">
                  For impersonated sessions initiated via the Stytch Dashboard, the impersonator's Stytch Dashboard `member_id`.
                </ResponseField>

                <ResponseField name="impersonator_email_address" type="string">
                  The email address of the impersonator.
                </ResponseField>
              </Expandable>
            </ResponseField>

            <ResponseField name="trusted_auth_token_factor" type="object">
              Information about the trusted auth token factor, if one is present.

              <Expandable title="properties">
                <ResponseField name="token_id" type="string">
                  The ID of the trusted auth token.
                </ResponseField>
              </Expandable>
            </ResponseField>

            <ResponseField name="oauth_access_token_exchange_factor" type="object">
              Information about the access token exchange factor, if one is present.

              <Expandable title="properties">
                <ResponseField name="client_id" type="string">
                  The ID of the Connected App client.
                </ResponseField>
              </Expandable>
            </ResponseField>
          </Expandable>
        </ResponseField>

        <ResponseField name="organization_id">
          The globally unique UUID that identifies the Organization associated with the Session.
        </ResponseField>

        <ResponseField name="organization_slug">
          The unique URL slug of the Organization associated with the Session.
        </ResponseField>

        <ResponseField name="roles" type="array[string]">
          A list of the roles associated with the Session.
          Members may inherit certain roles depending on the factors in their Session.
          For example, some roles may only be active if the member logged in from a specific SAML IDP.
        </ResponseField>

        <ResponseField name="started_at" type="string">
          The timestamp when the Session was created. Values conform to the RFC 3339 standard and are expressed in UTC, e.g. `2021-12-29T12:33:09Z`.
        </ResponseField>

        <ResponseField name="last_accessed_at" type="string">
          The timestamp when the Session was last accessed. Values conform to the RFC 3339 standard and are expressed in UTC, e.g. `2021-12-29T12:33:09Z`.
        </ResponseField>

        <ResponseField name="expires_at" type="string">
          The timestamp when the Session expires. Values conform to the RFC 3339 standard and are expressed in UTC, e.g. `2021-12-29T12:33:09Z`.
        </ResponseField>

        <ResponseField name="custom_claims" type="map<string, any>">
          The custom claims map for a Session. Claims can be added to a Session during a Sessions authenticate call.
        </ResponseField>
      </Expandable>
    </ResponseField>
  </Expandable>
</ResponseField>

## Return value

<ResponseField name="unsubscribe" type="function">
  The function to call to unsubscribe from the Organization change event.
</ResponseField>

<Panel>
  <RequestExample>
    ```jsx theme={null}
    import { useEffect, useState } from 'react';
    import { useStytchB2BClient } from '@stytch/nextjs/b2b';

    export const SessionDisplay = () => {
      const stytch = useStytchB2BClient();
      const [session, setSession] = useState(null);

      useEffect(() => {
        // Subscribe to session changes
        const unsubscribe = stytch.session.onChange((session) => {
          setSession(session);
        });

        // Cleanup subscription on unmount
        return unsubscribe;
      }, [stytch]);

      return (
        <div>
          {session ? (
            <p>Session ID: {session.member_session_id}</p>
          ) : (
            <p>No active session</p>
          )}
        </div>
      );
    };
    ```
  </RequestExample>
</Panel>
