> 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/onetalk-omnichannel-documentation/onetalk-channel-integration/live-chat/onetalk-live-chat-for-react-native/authentication-ios.md). Agents can also search and read this documentation through its [MCP server](https://docs.taptalk.io/~gitbook/mcp).

# Authentication - iOS

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 **`navigateToOneTalk`**. To do this, you can add an authentication method in the previously created module file. (See [Get Started - iOS](https://docs.taptalk.io/pages/-MQV25u4ATdW02mvTwhv#configure-and-initialize-taptalk.io-omnichannel-ios-sdk-for-react-native-project)). We will add a `RCT_EXPORT_METHOD` named **`authenticateUser`**.

You can start by adding an authentication method in **AppDelegate.m**.

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

```objective-c
// Method to authenticate user
- (void)authenticateUserWithFullName:(NSString *)fullName 
                               email:(NSString *)email 
                             success:(void (^)(NSString *message))success 
                               error:(void (^)(NSString *message))error {

    [[TapTalkLive sharedInstance] authenticateUserWithFullName:fullName 
                                                         email:email 
    success:^(NSString *message) {
        success(message);
    } 
    failure:^(NSError *error) {
        error(error.localizedDescription);
    }];
}
```

{% endcode %}
{% endtab %}

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

```swift
// Method to authenticate user
public func authenticateUser(
    fullName: String,
    email: String,
    success: @escaping (String) -> Void,
    failure: @escaping (String) -> Void
) {
    TapTalkLive.sharedInstance().authenticateUser(
        withFullName: fullName,
        email: email
    ) { message in
        success(message ?? "Successfully authenticated user")
    }
    failure: { error in
        failure(error?.localizedDescription ?? "Failed to authenticate user")
    }
}
```

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

{% hint style="info" %}
**Parameters**\
**`fullName`**: (String) current user's full name\
**`email`**: (String) current user's email address\
**`success`**: (void (^)(String message)) success callback containing a message\
**`error`**: (void (^)(String message)) success callback containing the error message
{% endhint %}

For Objective-C, open your **AppDelegate.h** file and add the following lines to enable the previously created method to be called from another file.

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

<pre class="language-objective-c" data-title="AppDelegate.h"><code class="lang-objective-c">@interface AppDelegate : UIResponder &#x3C;UIApplicationDelegate, RCTBridgeDelegate>

<strong>- (void)authenticateUserWithFullName:(NSString *)fullName 
</strong><strong>                               email:(NSString *)email 
</strong><strong>                             success:(void (^)(NSString *message))success 
</strong><strong>                               error:(void (^)(NSString *message))error;
</strong>
@end
</code></pre>

{% endtab %}
{% endtabs %}

Then open the previously created **OneTalkStarterModule.m** and add a method to bridge the authentication method from AppDelegate to JavaScript.

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

```objective-c
...

RCT_EXPORT_METHOD(authenticateUser:(NSString *)fullName
                             email:(NSString *)email
                   successCallback:(RCTResponseSenderBlock)successCallback
                     errorCallback:(RCTResponseSenderBlock)errorCallback)
{
    dispatch_async(dispatch_get_main_queue(), ^{
        AppDelegate *appDelegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
        [appDelegate authenticateUserWithFullName:fullName 
                                            email:email 
        success:^(NSString *message) {
            successCallback(@[message]);
        }
        failure:^(NSString *message) {
            errorCallback(@[message]);
        ];
    });
}
```

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

You can then call the method from JavaScript as such:

```javascript
NativeModules.OneTalkStarter.authenticateUser(
    'USER_NAME', 
    'USER_EMAIL',
    (successMessage) => {
        // Successfully authenticated user
    },
    (errorMessage) => {
       // An error occurred during authentication
    }
);
```

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

### Logout and Clear TapTalkLive Data

You may also add another method when you need to logout and clear all local cached data from Omnichannel SDK. We will name this method **`clearAllTapLiveData`**.

Start by adding an authentication method in **AppDelegate.m** and **AppDelegate.h**.

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

```objective-c
// Method to clear all live chat data
- (void)clearAllTapLiveData {
    [[TapTalkLive sharedInstance] logoutAndClearAllTapLiveData:^(NSString *message) {
        // Successfully logged out and cleared all TapTalkLive data
    }
    failure:^(NSError *error) {
        // An error occured, but local data is still cleared
    }];
}
```

{% endcode %}
{% endtab %}

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

```swift
// Method to clear all live chat data
public func clearAllTapLiveData {
    TapTalkLive.sharedInstance().logoutAndClearAllTapData { message in
        // Successfully logged out and cleared all TapTalkLive data
    }
    failure: { error in
        // An error occured, but local data is still cleared
    }
}
```

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

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

<pre class="language-objective-c" data-title="AppDelegate.h"><code class="lang-objective-c">@interface AppDelegate : UIResponder &#x3C;UIApplicationDelegate, RCTBridgeDelegate>

<strong>- (void)clearAllTapLiveData;
</strong>
@end
</code></pre>

{% endtab %}
{% endtabs %}

Then add the method to **OneTalkStarterModule.m**.

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

```objective-c
...

RCT_EXPORT_METHOD(clearAllTapLiveData)
{
    dispatch_async(dispatch_get_main_queue(), ^{
        AppDelegate *appDelegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
        [appDelegate clearAllTapLiveData];
    });
}
```

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

You can then call the method from JavaScript when needed.

```javascript
NativeModules.OneTalkStarter.clearAllTapLiveData();
```
