Documentation Index
Fetch the complete documentation index at: https://www.dynamic.xyz/docs/llms.txt
Use this file to discover all available pages before exploring further.
Refresh Authentication
refreshAuth refreshes the current user’s authentication data from the server. This function fetches the latest user information and token from the backend, updating both the local user and token states with any changes.
Use this when you need to ensure the user’s authentication state is synchronized with the server, such as after making changes to the user’s account or when the token needs to be refreshed.
Usage
import { refreshAuth } from '@dynamic-labs-sdk/client';
const updateAuth = async () => {
const response = await refreshAuth();
console.log('Auth refreshed:', response);
};
Parameters
| Parameter | Type | Description |
|---|
client | DynamicClient (optional) | The Dynamic client instance. Only required when using multiple clients. |
Returns
Promise<VerifyResponse> - A promise that resolves to the verify response containing updated user and token data.
Examples
Basic usage
import { refreshAuth } from '@dynamic-labs-sdk/client';
const handleProfileUpdate = async () => {
// After updating user profile on the backend
await updateUserProfile();
// Refresh auth to get the latest user data and token
await refreshAuth();
};
import { refreshAuth } from '@dynamic-labs-sdk/client';
function ProfileUpdateButton() {
const handleUpdate = async () => {
await updateUserProfile();
await refreshAuth();
// userChanged event fires — any useUser() consumers re-render automatically
};
return <button onClick={handleUpdate}>Update profile</button>;
}
Listen to auth changes
import { refreshAuth, onEvent } from '@dynamic-labs-sdk/client';
// Listen for token changes
onEvent({
event: 'tokenChanged',
listener: ({ token }) => {
console.log('Token updated:', token);
},
});
// Listen for user changes
onEvent({
event: 'userChanged',
listener: ({ user }) => {
console.log('User updated:', user);
},
});
// Trigger refresh - will fire both events if data changed
await refreshAuth();
In React, use useEvent from @dynamic-labs-sdk/react-hooks to subscribe to userChanged and tokenChanged — your components automatically re-render when refreshAuth fires those events. No manual listener setup needed in components.import { useEvent } from '@dynamic-labs-sdk/react-hooks';
// Register a one-off side-effect listener at app level if needed
function useAuthChangeLogger() {
useEvent({
event: 'tokenChanged',
listener: ({ token }) => console.log('Token updated:', token),
});
}