B2B Saas Authentication

/

Frontend SDKs

/

Headless

/

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 useStytchMemberSession hook is available in @stytch/react or @stytch/nextjs. It implements these methods for you to easily access the session and listen for changes.

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

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

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