Skip to main content

Summary

Used to manually refresh both the user state and authentication token at any point in time. This hook is useful when you need to ensure the user’s authentication data is synchronized with the server, such as after making changes to the user’s account or when the JWT token needs to be refreshed. Unlike useRefreshUser, which only refreshes user data, useRefreshAuth refreshes both the user information and the JWT token, ensuring complete authentication state synchronization. The SDK state as a whole will be maintained i.e. the sdkHasLoaded boolean on useDynamicContext will stay true. If you want to reset the whole SDK including wallets, then take a look at useReinitialize.

Usage

import {
  DynamicContextProvider,
  useRefreshAuth,
} from '@dynamic-labs/sdk-react-core';

const RefreshAuthButton = () => {
  const refreshAuth = useRefreshAuth();

  const handleRefresh = async () => {
    try {
      const user = await refreshAuth();
      console.log('Auth refreshed:', user);
    } catch (error) {
      console.error('Failed to refresh auth:', error);
    }
  };

  return <button onClick={handleRefresh}>Refresh Auth</button>;
};

const App = () => {
  return (
    <DynamicContextProvider>
      <RefreshAuthButton />
    </DynamicContextProvider>
  );
};

Return Value

Returns a callback function that:
  • Returns Promise<UserProfile | undefined> when called
  • Refreshes both user data and JWT token from the server
  • Updates the SDK state with the latest authentication information
  • Throws an error if the refresh results in an invalid user state

FAQs

When exactly is the JWT updated?
  • The JWT is updated on every successful call. On failure, nothing is mutated, so the existing JWT remains as-is (still usable until it actually expires)
Is there a client-side rate limit?
  • No, there’s no client-side limit, so it would come down to general rate limits at that point.
What’s the difference between useRefreshAuth and useRefreshUser?
  • useRefreshAuth refreshes both user data AND the JWT token from the server
  • useRefreshUser only refreshes user data without updating the JWT token
  • Use useRefreshAuth when you need to ensure complete authentication state synchronization
  • Use useRefreshUser when you only need to sync user profile information