Once a Member has successfully logged in, the SDK can be used to view and manage that Member's information along with information about the Organization they belong to.
Member
Methods
To call these methods, Manage members must be enabled in the SDK Configuration page of the Stytch dashboard.
Get Member
The SDK provides two methods for getting a Member. The recommended approach is to use the synchronous method, organization.getMemberSync, and listen to changes with the organization.onMemberChange method.
If logged in, the organization.getMemberSync method returns the cached Member object. Otherwise it returns null. This method does not refresh the Member's data. 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 organization.onMemberChange 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.
The asynchronous method, organization.getMember, 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
{
"status_code": 200,
"request_id": "request-id-test-b05c992f-ebdc-489d-a754-c7e70ba13141",
"member_id": "member-test-32fc5024-9c09-4da3-bd2e-c9ce4da9375f",
"member": {...},
"organization": {...}
}
Update Member
The Update Member method wraps the Update Member API endpoint. Use this method to update the Member's name, untrusted metadata, MFA phone number, and MFA enrollment status.
You can listen for successful Member updates anywhere in the codebase with the stytch.organization.onMemberChange() method or useStytchMember hook if you are using React.
Method parameters
import React, { useCallback } from 'react';
import { useStytchB2BClient } from '@stytch/react';
export const UpdateMember = () => {
const stytchClient = useStytchB2BClient();
const updateName = useCallback(() => {
stytchClient.member.update({
name: 'Jane Doe',
untrusted_metadata: {
display_theme: 'DARK_MODE',
},
});
}, [stytchClient]);
return (
<>
<button onClick={updateName}>Update name</button>
</>
);
};
RESPONSE
{
"status_code": 200,
"request_id": "request-id-test-b05c992f-ebdc-489d-a754-c7e70ba13141",
"member_id": "member-test-32fc5024-9c09-4da3-bd2e-c9ce4da9375f",
"member": {...},
"organization": {...}
}
Delete Member MFA phone number
The Delete Member MFA phone number method wraps the Delete Member MFA phone number API endpoint. Use this method to delete the Member's MFA phone number.
To change a Member's phone number, you must first call this endpoint to delete the existing phone number.
Existing Member Sessions that include a phone number authentication factor will not be revoked if the phone number is deleted, and MFA will not be enforced until the Member logs in again. If you wish to enforce MFA immediately after a phone number is deleted, you can do so by prompting the Member to enter a new phone number and calling the SMS OTP send endpoint, then calling the SMS Authenticate endpoint.
You can listen for successful Member updates anywhere in the codebase with the stytch.organization.onMemberChange() method or useStytchMember hook if you are using React.
import React from 'react';
import { useStytchB2BClient } from '@stytch/react';
export const deleteMemberMFAPhoneNumber = () => {
const stytchClient = useStytchB2BClient();
const deleteMFAPhoneNumber = () => {
stytchClient.member.deleteMFAPhoneNumber();
};
return (
<>
<button onClick={deleteMFAPhoneNumber}>Delete MFA phone number</button>
</>
);
};
RESPONSE
{
"member": {...},
"member_id": "member-test-32fc5024-9c09-4da3-bd2e-c9ce4da9375f",
"organization": {...}
"request_id": "request-id-test-b05c992f-ebdc-489d-a754-c7e70ba13141",
"status_code": 200
}