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

export const RecoverWithCode = () => {
  const stytch = useStytchB2BClient();
  const [recoveryCode, setRecoveryCode] = useState('');

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

  return (
    <View>
      <TextInput
        value={recoveryCode}
        onChangeText={setRecoveryCode}
        placeholder="Enter recovery code"
      />
      <Button title="Recover Account" onPress={recover} />
    </View>
  );
};
recoveryCodes.recover wraps the Recovery Codes Recover API endpoint. It takes a single recovery_code parameter, which is a recovery code that was previously generated for the Member. Calling the recover endpoint will consume the recovery code and authenticate the Member, minting a new session for them. Currently, recovery codes are only generated when a enrolls in TOTP as their secondary MFA factor, and as such authenticate members in place of a stytch.totps.authenticate(). If neither a Member Session nor an is present, this method will fail.

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

Parameters

recover_code
string
required
The recovery code generated by a secondary MFA method. This code is used to authenticate in place of the secondary MFA method if that method as a backup.

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.