Skip to main content
import { useStytchB2BClient } from '@stytch/react/b2b';

export const ExchangeIntermediateSession = () => {
  const stytch = useStytchB2BClient();

  useEffect(() => {
    stytch.discovery.intermediateSessions.exchange({
      organization_id: 'organization-test-07971b06-ac8b-4cdb-9c15-63b17e653931',
      session_duration_minutes: 60,
    });
  }, [stytch]);

  return (
    <button onClick={exchangeIntermediateSession}>Log into Organization</button>
  );
};
{
  "request_id": "request-id-test-b05c992f-ebdc-489d-a754-c7e70ba13141",
  "status_code": 200,
  "member_id": "member-test-32fc5024-9c09-4da3-bd2e-c9ce4da9375f",
  "session_token": "mZAYn5aLEqKUlZ_Ad9U_fWr38GaAQ1oFAhT8ds245v7Q",
  "session_jwt": "eyJ...",
  "intermediate_session_token": "",
  "member_authenticated": true,
  "mfa_required": null,
  "primary_required": null,
    "member_session": {...},
    "member": {...},
    "organization": {...}
}
The discovery.intermediateSessions.exchange method wraps the Exchange Intermediate Session endpoint. This method will fail if there is no intermediate session token stored the browser. Intermediate session tokens are generated upon successful calls to primary authenticate methods in the case where MFA is required, such as email magic link authenticate, or upon successful calls to discovery authenticate methods, such as email magic link discovery authenticate.

If this method succeeds and the Member is not required to complete MFA, the Member will be logged in, granted an active session, and the .

If this method succeeds and MFA is required, the intermediate session token will be stored .
Use the to listen for successful login events anywhere in codebase.

Parameters

session_duration_minutes
number
required
Set the session lifetime to be this many minutes from now. This will return both an opaque session_token and session_jwt for this session, which will automatically be stored in the browser cookies. The session_jwt will have a fixed lifetime of five minutes regardless of the underlying session duration, and will be automatically refreshed by the SDK in the background over time.This value must be a minimum of 5 and may not exceed the maximum session duration minutes value set in the Stytch Dashboard.A successful authentication will continue to extend the session this many minutes.
organization_id
string
required
The id of the Organization that the Member is joining.
locale
string
The locale to use if an OTP code is sent to the member’s phone number as part of a secondary authentication requirement.

Response

member_id
string
The unique ID of the Member.
member_session
object
The new Stytch Session.
member
object
The Member object of the signed-in Member.
organization
object
The Organization object of the Organization the Member has signed into.
session_jwt
string
The JWT for the new Stytch Session.
If the project is configured to use HttpOnly cookies, this field will always be an empty string.
session_token
string
The secret token for the new Stytch Session.
If the project is configured to use HttpOnly cookies, this field will always be an empty string.
member_device
object
If Protected Auth is enabled and returned fingerprinting results, this field will contain information about the member’s device attributes.
member_authenticated
boolean
Indicates whether the Member is fully authenticated. If false, the Member needs to complete an MFA step to log in to the Organization.
intermediate_session_token
string
The intermediate_session_token that should be passed into a secondary authentication endpoint, such as OTP authenticate, in order to receive a member session. The intermediate_session_token can also be used with discovery endpoints to join a different organization or create a new organization.If the member is fully authenticated, this field will be an empty string.
If the project is configured to use HttpOnly cookies, this field will always be an empty string.
primary_required
object | null
The types of primary authentication required.
mfa_required
object | null
The types of secondary authentication required to join the Organization.
member_device
object
If Protected Auth is enabled and returned fingerprinting results, this field will contain information about the member’s device attributes.
request_id
string
Globally unique UUID that is returned with every API call. This value is important to log for debugging purposes; we may ask for this value to help identify a specific API call when helping you debug an issue.
status_code
number
The HTTP status code of the response. Stytch follows standard HTTP response status code patterns, e.g. 2XX values equate to success, 3XX values are redirects, 4XX are client errors, and 5XX are server errors.