> ## 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

# 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

```javascript theme={"system"}
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

<Tabs>
  <Tab title="JavaScript">
    ```javascript theme={"system"}
    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();
    };
    ```
  </Tab>

  <Tab title="React">
    ```tsx theme={"system"}
    import { useRefreshAuth } from '@dynamic-labs-sdk/react-hooks';

    function ProfileUpdateButton() {
      const { mutate: refreshAuth, isPending } = useRefreshAuth();

      const handleUpdate = async () => {
        await updateUserProfile();
        refreshAuth();
        // userChanged event fires — any useUser() consumers re-render automatically
      };

      return (
        <button onClick={handleUpdate} disabled={isPending}>
          Update profile
        </button>
      );
    }
    ```
  </Tab>
</Tabs>

### Listen to auth changes

<Tabs>
  <Tab title="JavaScript">
    ```javascript theme={"system"}
    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();
    ```
  </Tab>

  <Tab title="React">
    In React, use `useOnEvent` 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.

    ```tsx theme={"system"}
    import { useOnEvent } from '@dynamic-labs-sdk/react-hooks';

    // Register a one-off side-effect listener at app level if needed
    function useAuthChangeLogger() {
      useOnEvent({
        event: 'tokenChanged',
        listener: ({ token }) => console.log('Token updated:', token),
      });
    }
    ```
  </Tab>
</Tabs>
