> For the complete documentation index, see [llms.txt](https://docs.taptalk.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.taptalk.io/powertalk-chat-sdk-documentation/powertalk-ios/authentication.md).

# Authentication

In order to use the abilities of the iOS SDK in your client app, a TapTalk instance must be initiated in each client app through user authentication with TapTalk.io server. An authenticated user account allows the instance to communicate and interact with the server. The following explains how to authenticate your user with the server.

![TapTalk.io's Authentication Flow](https://4266585843-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LfVupFSqh_qZAY9OiCO%2F-MHKsq4vOlouRa03adbb%2F-MHLTpHao9rfmA05gZO6%2F3cee4ff-Login_Flow2x.png?alt=media\&token=e252a825-bf44-49ef-954d-f2c4a841f154)

When the user logs in to your application, your backend server will have to request an **Authentication Ticket** from TapTalk.io server. TapTalk.io server will return the requested **Authentication Ticket** and **user ID** to your backend server. The client application (iOS or Android) can then retrieve the ticket from your backend server to implement TapTalk.io's SDK by initializing the SDK using the obtained **Authentication Ticket**.

You can see the documentation on how to obtain Authentication Ticket from TapTalk.io's Server on the [Authentication in TapTalk.io's Server API page](https://docs.taptalk.io/powertalk-chat-sdk-documentation/powertalk-server-api/authentication.md).

### Authenticate to TapTalk.io server with an Authentication Ticket

{% hint style="warning" %}
**Note:** authentication should only be called once before the user is logged in or when the callback tapTalkRefreshTokenExpired is triggered in [TapTalkDelegate](https://docs.taptalk.io/powertalk-chat-sdk-documentation/powertalk-ios/event-delegate.md#taptalkdelegate)
{% endhint %}

{% tabs %}
{% tab title="Objective-C" %}
{% code title="AppDelegate.m" %}

```objective-c
// For version 3.0.0 and above
#import "PowerTalk.h"

// For versions below 3.0.0
#import <TapTalk/TapTalk.h>
  
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Other Code
    
    [[TapTalk sharedInstance] authenticateWithAuthTicket:AUTH_TICKET
                                      connectWhenSuccess:CONNECT_WHEN_SUCCESS
    success:^{
        // Successfully authenticated
    }
    failure:^(NSError *error) {
        // Failed to authenticate
    }];
}
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code title="AppDelegate.swift" %}

```swift
import PowerTalk

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
    
    // Other Code
    
    TapTalk.sharedInstance().authenticate(
        withAuthTicket: AUTH_TICKET,
        connectWhenSuccess: CONNECT_WHEN_SUCCESS
    ) {
        // Successfully authenticated
    }
    failure: { error in
        // Failed to authenticate
    }
    
    return true
}
```

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

{% hint style="info" %}
**Parameters**\
`AUTH_TICKET` : (String) an authentication ticket is required for this method\
`CONNECT_WHEN_SUCCESS`: (boolean) if set to true, application will connect to TapTalk.io server after authentication is successfully completed
{% endhint %}

### Check Authentication Status

You can check authentication status by calling **`isAuthenticated`** method

{% tabs %}
{% tab title="Objective-C" %}

```objective-c
// For version 3.0.0 and above
#import "PowerTalk.h"

// For versions below 3.0.0
#import <TapTalk/TapTalk.h>

BOOL isAuthenticated = [[TapTalk sharedInstance] isAuthenticated];
```

{% endtab %}

{% tab title="Swift" %}

```swift
import PowerTalk

var isAuthenticated = TapTalk.sharedInstance().isAuthenticated()
```

{% endtab %}
{% endtabs %}

### Logout and Clear TapTalk.io Data

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

{% tabs %}
{% tab title="Objective-C" %}

```objective-c
// For version 3.0.0 and above
#import "PowerTalk.h"

// For versions below 3.0.0
#import <TapTalk/TapTalk.h>

// Call to logout and clear all local data from TapTalk.io
[[TapTalk sharedInstance] logoutAndClearAllTapTalkData];
```

{% endtab %}

{% tab title="Swift" %}

```swift
import PowerTalk

TapTalk.sharedInstance().logoutAndClearAllTapTalkData()
```

{% endtab %}
{% endtabs %}

### Clear TapTalk.io Data

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

{% tabs %}
{% tab title="Objective-C" %}

```objective-c
// For version 3.0.0 and above
#import "PowerTalk.h"

// For versions below 3.0.0
#import <TapTalk/TapTalk.h>

[[TapTalk sharedInstance] clearAllTapTalkData];
```

{% endtab %}

{% tab title="Swift" %}

```swift
import PowerTalk

TapTalk.sharedInstance().clearAllTapTalkData()
```

{% endtab %}
{% endtabs %}
