Skip to main content
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

type
string
required
Always 'authTokenNeeded'.
authToken
string | null
required
The expired token, or null if unavailable. You must set this property to the new token.

Example

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

Do you need practical examples?

Learn how to apply the features available for embedded chat into your implementation with guidance and examples.