> ## Documentation Index
> Fetch the complete documentation index at: https://developer.watson-orchestrate.ibm.com/llms.txt
> Use this file to discover all available pages before exploring further.

# authTokenNeeded

Triggered when the JWT authentication token expires and needs to be refreshed. Use this event to provide a new token to maintain secure communication with the backend.

## Event properties

<ParamField path="type" type="string" required>
  Always `'authTokenNeeded'`.
</ParamField>

<ParamField path="authToken" type="string | null" required>
  The expired token, or `null` if unavailable. You must set this property to the new token.
</ParamField>

## Example

```javascript theme={null}
instance.on('authTokenNeeded', async (event, instance) => {
    console.log('Auth token expired, refreshing...');
    
    try {
        // Fetch new token from your authentication service
        const response = await fetch('/api/auth/refresh', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ oldToken: event.authToken })
        });
        
        const data = await response.json();
        
        // Provide the new token
        event.authToken = data.newToken;
        
        console.log('Token refreshed successfully');
    } catch (error) {
        console.error('Failed to refresh token:', error);
        
        // Handle refresh failure (e.g., redirect to login)
        redirectToLogin();
    }
});
```

<Card title="Do you need practical examples?" icon="text-align-start" href="https://www.ibm.com/docs/SSAVQO/deploy/web_chat_agent/tutorials/tutorials_lp.html" arrow="true" cta="Learn with walk-throughs" horizontal>
  Learn how to apply the features available for embedded chat into your implementation with guidance and examples.
</Card>
