B2B Saas Authentication

/

Frontend SDKs

/

Headless

/

Members

/

Get Member

Get Member

The SDK provides synchronous and asynchronous methods for getting a Member. The recommended approach is to use the synchronous method, member.getSync, and listen to changes with the member.onChange method.

If logged in, the member.getSync method returns the cached Member object. Otherwise it returns null. This method does not refresh the Member's data.

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

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

In React, the @stytch/react library provides the useStytchMember hook that implements these methods for you to easily access the Member and listen for changes.

The asynchronous method, member.get, wraps the search member endpoint. It fetches the Member's data and refreshes the cached object if changes are detected. The Stytch SDK will invoke this method automatically in the background, so you probably won't need to call this method directly.

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

export const Home = () => {
  const { member } = useStytchMember();

  return member ? <p>Welcome, {member.name}</p> : <p>Log in to continue!</p>;
};

RESPONSE

200
{
  "status_code": 200,
  "request_id": "request-id-test-b05c992f-ebdc-489d-a754-c7e70ba13141",
  "member_id": "member-test-32fc5024-9c09-4da3-bd2e-c9ce4da9375f",
  "member": {...},
  "organization": {...}
}