Skip to main content

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

ParameterTypeDescription
clientDynamicClient (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();
};

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();