Consumer Authentication

/

Mobile SDKs

/

React Native SDK reference

/

One-time Passcodes (OTP)

/

Authenticate

Authenticate

The Authenticate method wraps the authenticate one-time passcode API method which validates the code passed in.

If this method succeeds, the user will be logged in, granted an active session, and the session data will be persisted on device.

You can listen for successful login events anywhere in the codebase with the stytch.session.onChange() method or useStytchSession hook.


Method parameters


method_id*string

code*string

Configuration*object

Additional configuration.

session_duration_minutes*int

Response fields


request_idstring

status_codeint

method_idstring

user_idstring

session_tokenstring

session_jwtstring

sessionobject
import { useStytch } from '@stytch/react-native';
import React, { useCallback, useState } from 'react';
import { Text, TextInput, TouchableOpacity, View } from 'react-native';

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

  const method_id = 'phone-number-test-d5a3b680-e8a3-40c0-b815-ab79986666d0';
  // returned from calling loginOrCreate for OTPs on SMS, WhatsApp or Email

  const authenticate = useCallback(
    (e) => {
      e.preventDefault();
      stytch.otps.authenticate(code, method_id, {
        session_duration_minutes: 60,
      });
    },
    [stytch, code],
  );

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

  return (
    <View>
      <Text>Enter code</Text>
      <TextInput value={code} onChangeText={handleChange} keyboardType="numeric" autoComplete="one-time-code" />
      <TouchableOpacity onPress={authenticate}>
        <Text>Submit</Text>
      </TouchableOpacity>
    </View>
  );
};

RESPONSE

200
{
  "status_code": 200,
  "request_id": "request-id-test-b05c992f-ebdc-489d-a754-c7e70ba13141",
  "user_id": "user-test-16d9ba61-97a1-4ba4-9720-b03761dc50c6",
  "session_token": "mZAYn5aLEqKUlZ_Ad9U_fWr38GaAQ1oFAhT8ds245v7Q",
  "session_jwt": "eyJ...",
  "session": {...},
  "method_id": "phone-number-test-d5a3b680-e8a3-40c0-b815-ab79986666d0",
}