B2B Saas Authentication

/

Mobile SDKs

/

React Native SDK reference

/

Session Management

/

Get Session

Get session

The SDK provides the session.getSync method to retrieve the current session. The session.onChange method can be used to listen to session changes.

If logged in, the session.getSync method returns the cached session object. Otherwise it returns null.

The session.getInfo method is similar to session.getSync, but it returns an object containing the session object and a fromCache boolean. If fromCache is true, the session object is from the cache and a state refresh is in progress.

The session.onChange method takes in a callback that gets called whenever the Member Session object changes. It returns an unsubscribe method for you to call when you no longer want to listen for such changes.

The @stytch/react-native library also provides the useStytchMemberSession hook that implements these methods for you to easily access the session and listen for changes.

import React from 'react';
import { Text } from 'react-native';
import { useStytchMemberSession } from '@stytch/react-native/b2b';

export const Home = () => {
  const { session } = useStytchMemberSession();

  return session ? <Text> Member Session ID: {session.member_session_id}</Text> : <Text> No Active Session </Text>;
};