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

export const DiscoveryAuthenticate = () => {
  const stytch = useStytchB2BClient();
  const [code, setCode] = useState('');

  // email used during discovery send
  const email = 'user@example.com';

  const authenticate = async () => {
    const { intermediate_session_token, email_address, discovered_organizations } =
      await stytch.otps.email.discovery.authenticate({
        email_address: email,
        code
      });
    console.log({ intermediate_session_token, email_address, discovered_organizations });
  };

  const handleChange = useCallback((e) => {
    setCode(e.target.value);
  }, []);

  return (
    <form>
      <label htmlFor="otp-input">Enter code</label>
      <input
        id="otp-input"
        autoComplete="one-time-code"
        inputMode="numeric"
        pattern="[0-9]*"
        onChange={handleChange}
      />
      <button onClick={authenticate} type="submit">
        Submit
      </button>
    </form>
  );
};
{
  "request_id": "request-id-test-b05c992f-ebdc-489d-a754-c7e70ba13141",
  "status_code": 200,
  "intermediate_session_token": "SeiGwdj5lKkrEVgcEY3QNJXt6srxS3IK2Nwkar6mXD4=",
  "email_address": "sandbox@stytch.com",
  "discovered_organizations": [{...}, {...}]
}
otps.email.discovery.authenticate wraps the Authenticate Discovery OTP endpoint, which validates the OTP code passed in. Use this method to authenticate a discovery Email OTP. If this method succeeds, the token will be stored . After authenticating a discovery OTP, you’ll receive an Intermediate Session that contains a list of discovered organizations. The user can then select an organization and exchange the intermediate session for a full session using the exchange intermediate session method.

Parameters

code
string
required
The code to authenticate.
email_address
string
required
The email address of the Member.

Response

email_address
string
The email address that was authenticated.
discovered_organizations
object[]
An array of discovered Organizations that the Member can authenticate into.
intermediate_session_token
string
The intermediate session token that can be exchanged for a full session.
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.