# How to deal with activation success or failure?

Failures happen for various reasons, therefore while creating robust and secure system the implementation needs to cope with such cases. Authentication failures can happen in two places:

## **The sign-in in the visitor’s authenticated space fails**

\
In this case, the backend logic is unable to generate a token : it is then the client’s responsibility to activate the iAdvize tag. In other words, it is the client implementation responsibility to decide whether:

* Not to activate the tag (i.e. not calling "iAdvize.activate" methods)
* Or to fall back to an anonymous activation instead.

If you want to ensure a fully authenticated conversational experience, we recommend the first defensive implementation.

## **The iAdvize "activate" method can also fail** <a href="#h_01hb65sg58h9va44qbax6jbake" id="h_01hb65sg58h9va44qbax6jbake"></a>

If the token is ill-formatted or if signature is incorrect, the "iAdvize.activate" can also fail.

To cope with this, the "iAdvize.activate" method takes an optional argument : a function that will be called with an object containing the result of the authentication ("authentication-success" or "authentication-failure"). This object includes the reason the authentication failed: a malformed token, an invalid key, a double login attempt, ...

```javascript
const iAdvizeActivationCallback = (activation) => {
        console.log(activation);
}
/* In case of success, logs :
{
        authentication: {
                option: { type: 'SECURED_AUTHENTICATION', token: '<YOUR-TOKEN>' },
                status: 'authentication-success'
        }
}

/* In case of failure, logs :
{
        authentication: {
                option: { type: 'SECURED_AUTHENTICATION', token: '<YOUR-TOKEN>' },
                status: 'authentication-failure',
                reason: 'A login is already ongoing' // Or another relevant error
        }
}

iAdvizeInterface.push(async (iAdvize) => {
        const activation = await iAdvize.activate(async () => {
                const token = await ... // your backend logic to generate a JWE - note that this can be called at anytime for refresh
                return {
                        authenticationOption: { type: 'SECURED_AUTHENTICATION', token }
                };
        }) ;
        iAdvizeActivationCallback(activation);
});
```

In case of failure, we recommend that you do not fallback to the anonymous activation.

**Note** : iAdvize javascript logic has already retry policies implemented. It will automatically retry 3 times in case of error. If it does not succeed, iAdvize systems return an error. We do not recommend that the client implementation adds additional retries.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.iadvize.dev/use-cases/visitor-experience/authenticated-messaging/web-client-side-implementation/how-to-deal-with-activation-success-or-failure.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
