# Authentication

TapTalk.io Omnichannel SDK provides some essential methods to handle user authentication.

### Authenticate User (Optional)

You can authenticate the user that is currently logged in before calling **`openTapTalkLiveView`**. To do this, provide the user's full name and email by calling **`authenticateUser`** from the **TapTalkLive** class after initializing the SDK.

{% tabs %}
{% tab title="Java" %}
{% code title="YourActivity.java" %}

```java
import io.taptalk.taptalklive.TapTalkLive;
import io.taptalk.taptalklive.Listener.TTLCommonListener;

...

TapTalkLive.requestAccessToken(FULL_NAME, EMAIL, new TTLCommonListener() {
    @Override
    public void onSuccess(String successMessage) {
        // Authentication successful
    }

    @Override
    public void onError(String errorCode, String errorMessage) {
        
    }
});
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code title="YourActivity.kt" %}

```kotlin
import io.taptalk.taptalklive.TapTalkLive
import io.taptalk.taptalklive.Listener.TTLCommonListener

...

TapTalkLive.authenticateUser(FULL_NAME, EMAIL, object : TTLCommonListener() {
    override fun onSuccess(successMessage: String?) {
        // Authentication successful
    }

    override fun onError(errorCode: String?, errorMessage: String?) {
        
    }
})
```

{% endcode %}
{% endtab %}
{% endtabs %}

{% hint style="info" %}
**Parameters**\
\&#xNAN;**`NAME`**: (String) current user's full name\
\&#xNAN;**`EMAIL`**: (String) current user's email address
{% endhint %}

After successfully authenticated, user will no longer be required to fill name and email to create a case.

### Authenticate with Auth Ticket (Optional)

You can also authenticate using an existing auth ticket with **`requestAccessToken`** method from the **TapTalkLive** class after initializing the SDK.

{% tabs %}
{% tab title="Java" %}
{% code title="YourActivity.java" %}

```java
import io.taptalk.taptalklive.TapTalkLive;
import io.taptalk.taptalklive.Listener.TTLCommonListener;

...

TapTalkLive.requestAccessToken(AUTH_TICKET, new TTLCommonListener() {
    @Override
    public void onSuccess(String successMessage) {
        // Authentication successful
    }

    @Override
    public void onError(String errorCode, String errorMessage) {
        
    }
});
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code title="YourActivity.kt" %}

```kotlin
import io.taptalk.taptalklive.TapTalkLive
import io.taptalk.taptalklive.Listener.TTLCommonListener

...

TapTalkLive.requestAccessToken(AUTH_TICKET, object : TTLCommonListener() {
    override fun onSuccess(successMessage: String?) {
        // Authentication successful
    }

    override fun onError(errorCode: String?, errorMessage: String?) {
        
    }
})
```

{% endcode %}
{% endtab %}
{% endtabs %}

{% hint style="info" %}
**Parameters**\
\&#xNAN;**`AUTH_TICKET`**: (String) existing auth ticket retrieved from the server
{% endhint %}

### Logout

You can clear the currently authenticated user data using the **`logout`** method. After logout is finished, locally saved user and chat data will be cleared, and you may authenticate another user.

{% tabs %}
{% tab title="Java" %}

```java
import io.taptalk.taptalklive.TapTalkLive;
import io.taptalk.taptalklive.Listener.TTLCommonListener;

...

TapTalkLive.logout(new TTLCommonListener() {
    @Override
    public void onSuccess(String successMessage) {
        // Successfully logged out
    }

    @Override
    public void onError(String errorCode, String errorMessage) {
        // An error occured, but local data is still cleared
    }
});
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
import io.taptalk.taptalklive.TapTalkLive;
import io.taptalk.taptalklive.Listener.TTLCommonListener;

...

TapTalkLive.logout(object : TTLCommonListener() {
    override fun onSuccess(successMessage: String?) {
        // Successfully logged out
    }

    override fun onError(errorCode: String?, errorMessage: String?) {
        // An error occured, but local data is still cleared
    }
})
```

{% endtab %}
{% endtabs %}

### Logout and Clear TapTalkLive Data

Call this method when you need to logout and clear all local cached data from Omnichannel SDK.

{% tabs %}
{% tab title="Java" %}

```java
import io.taptalk.taptalklive.TapTalkLive;

...

TapTalkLive.clearAllTapLiveData();
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
import io.taptalk.taptalklive.TapTalkLive;

...

TapTalkLive.clearAllTapLiveData()
```

{% endtab %}
{% endtabs %}
