Consumer Authentication

/

Mobile SDKs

/

React Native SDK reference

/

Email Magic Links

/

Authenticate

Authenticate

The Authenticate method wraps the authenticate Magic Link API endpoint which validates the magic link token 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


token*string

Configuration*object

Additional configuration.

session_duration_minutes*int

Response fields


request_idstring

status_codeint

user_idstring

method_idstring

session_tokenstring

session_jwtstring

sessionobject
import React, { useEffect } from 'react';
import { Text } from 'react-native';
import { useStytch, useStytchSession } from '@stytch/react-native';
import { useNavigation } from '@react-navigation/native';

export const Authenticate = ({ token }) => {
  const stytch = useStytch();
  const { session } = useStytchSession();
  const navigation = useNavigation();

  useEffect(() => {
    const authenticateUser = async () => {
      if (session) {
        navigation.navigate('Profile');
      } else if (token) {
        try {
          await stytch.magicLinks.authenticate(token, {
            session_duration_minutes: 60,
          });
          navigation.navigate('Profile');
        } catch (error) {
          console.error('Authentication error:', error);
        }
      }
    };

    authenticateUser();
  }, [stytch, session, navigation, token]);

  return <Text>Loading</Text>;
};

RESPONSE

200
{
    "status_code": 200,
    "request_id": "request-id-test-b05c992f-ebdc-489d-a754-c7e70ba13141",
    "user_id": "user-test-16d9ba61-97a1-4ba4-9720-b03761dc50c6",
    "method_id": "email-test-81bf03a8-86e1-4d95-bd44-bb3495224953",
    "session_token": "mZAYn5aLEqKUlZ_Ad9U_fWr38GaAQ1oFAhT8ds245v7Q",
    "session_jwt": "eyJ...",
    "session": {...},
}