Skip to main content
import { useState } from 'react';
import { View, TextInput, Button } from 'react-native';
import { useStytchB2BClient } from '@stytch/react-native/b2b';

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

  const authenticate = async () => {
    const response = await stytch.otps.sms.authenticate({
      organization_id: 'organization-test-07971b06-ac8b-4cdb-9c15-63b17e653931',
      member_id: 'member-test-32fc5024-9c09-4da3-bd2e-c9698807585d',
      code: code,
      session_duration_minutes: 60,
    });
    console.log('Authenticated:', response);
  };

  return (
    <View>
      <TextInput
        value={code}
        onChangeText={setCode}
        placeholder="Enter OTP code"
      />
      <Button title="Verify OTP" onPress={authenticate} />
    </View>
  );
};
otps.sms.authenticate wraps the Authenticate SMS OTP API endpoint. If there is a current Session, the SDK will call the endpoint with the session token. This will add the phone number factor to the existing Member Session. Otherwise, the SDK will use the intermediate session token. This will consume the intermediate session token and create a Member Session. 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 neither a Member Session nor an intermediate session token is present, this method will fail.

If this method succeeds, the Member will be logged in, granted an active session, and the .

Parameters

organization_id
string
required
Globally unique UUID that identifies a specific Organization. You may also use the organization_slug or organization_external_id here as a convenience.
member_id
string
required
Globally unique UUID that identifies a specific Member. You may use an external_id here if one is set for the member.
code
string
required
The OTP to authenticate.
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 Frontend SDK page of the Stytch Dashboard.A successful authentication will continue to extend the session this many minutes.
set_mfa_enrollment
string
Optionally sets the Member’s MFA enrollment status upon a successful authentication. If the Organization’s MFA policy is REQUIRED_FOR_ALL, this field will be ignored. If this field is not passed in, the Member’s mfa_enrolled boolean will not be affected. The options are:
  • enroll: sets the Member’s mfa_enrolled boolean to true. The Member will be required to complete an MFA step upon subsequent logins to the Organization.
  • unenroll: sets the Member’s mfa_enrolled boolean to false. The Member will no longer be required to complete MFA steps when logging in to the Organization.

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.
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.