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

# 3. FID Authentication

### 3.1 FTSDKAuthDelegate

Before using Authentication functions, please make sure you have implemented `FTSDKAuthDelegate`&#x20;

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

```objectivec
// MARK: FTSDKAuthDelegate

- (void)didSignInSuccess:(FTSDKSignIn *)signIn didSignInFor:(FTSDKUser *)user withMethod:(NSString *)authType {
    // Call when user successful login, and every time user open app again
}

- (void)didSignInFail:(FTSDKSignIn *)signIn with:(FTSDKError *)error {
    // Call when user login failed
}

- (void)didSignUpSuccess:(FTSDKSignIn *)signIn didSignInFor:(FTSDKUser *)user withMethod:(NSString *)authType {
    // Call when user successful signup
}

- (void)didSignUpFail:(FTSDKSignIn *)signIn with:(FTSDKError *)error {
    // Call when user signup failed
}

- (void)onFTSDKIsReady {
    // Call when FID is ready, make sure you have wait before use any FID's functions
    
    // Example:
    // Verify Pending Transacions
    // Setup player info before make purchase
}

- (void)onFTSDKErrorWithError:(FTSDKError *)error {
    // Call if FID init error
    // You need to block the user to continue using app/game
}

- (void)onFIDMaintenanceWithConfig:(MaintenanceConfigs *)config {
    // Call if FID is on maintenance mode
    // You need to show an Maintenance dialog
    // A refresh button can be reload your app/game
}

- (void)onFIDLinkAccountSuggestionWithMessage:(NSString *)message {
    // If user using 3rd login (Apple, Google, Facebook), we need suggess user link their account with phone number
    // You need to show an dialog to suggess user link account
    // A button Link Account will call [[FTSDKSignIn instance] linkAccount];
    // View Link Account feature before implement this
}
```

{% endtab %}
{% endtabs %}

Asign delegate instance:

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

```
[FTSDKSignIn instance].delegate = self;
```

{% endtab %}
{% endtabs %}

### 3.2 Sign In & Sign Up

<figure><img src="/files/kjmjiDkxrWQwTv5ewFKU" alt=""><figcaption><p>Sign In/ Sign Up dialog</p></figcaption></figure>

After implement `FTSDKAuthDelegate`, you can start call `Sign In` or `Sign Up`

To show `Sign In & Sign Up` dialog, call:

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

```objectivec
[[FTSDKSignIn instance] signIn];
```

{% endtab %}
{% endtabs %}

Multiple authentication methods provided by FID:

* [x] Phone Number
* [x] Play Now
* [x] Facebook
* [x] Google
* [x] Apple

#### Change Product Name when login with Google & Facebook

When you use login with Google/Facebook, you will see "XXX" want to use facebook.com to....

If you want to change the text "XXX", please go to **Build Setting**, search **"product name"** then change the value to anything that you want.

And remember to add config to your **Info.plist**

```xml
<key>FacebookDisplayName</key>
<string>Your App/Game Name</string>
```

#### Setup login with Facebook

You will need Facebook App ID and Facebook Client ID Token (We will give you)

Add new configs to your Info.plist

* FacebookAppID
* FacebookClientToken
* FacebookUrlSchemeSuffix (optional)

Go to your Target -> Info and add new URL Types:

* fbxxx (xxx is FacebookAppID)

#### Setup login with Google

You will need **GoogleService-Info.plist** file (We well give you)

Drag file to your project, remember check **Copy If Need** option and choose Target

Go to your Target -> Info and add new URL Types:

* com.googleusercontent.apps.xxx-yyy (You can find it in **GoogleService-Info.plist** file)

#### Setup login with Apple

By default, when we give you certificate and profile, it will include InApp-Purchage and SignIn with Apple capability

You need add some Capability to enable functions:

* In-App Purchase
* Sign in with Apple

#### Setup Dynamiclink for KOLs

You will need setup some Associated Domain and deeplink, we will give you

### 3.3 Forgot Password

When user use Phone Number and Password, if forgot, they can change password by SMS OTP, just click `ForgotPassword` on `Sign In & Sign Up` dialog

### 3.4 Link Account

If a user is using a third-party login such as Apple, Google, Facebook, or PlayNow, we suggest linking their account with a phone number.

Before call link account, you need implement `FTSDKLinkAccountDelegate` first:

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

```
// MARK: FTSDKLinkAccountDelegate

- (void)didLinkAccountSuccessWithUser:(FTSDKUser *)user {
    // Call if link account success
}

- (void)didLinkAccountFailWithError:(FTSDKError *)error {
    // Call if link account failed
}
```

{% endtab %}
{% endtabs %}

Asign delegate instance:

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

```
[FTSDKSignIn instance].linkAccountDelegate = self;
```

{% endtab %}
{% endtabs %}

Start link account:

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

```
[[FTSDKSignIn instance] linkAccount];
```

{% endtab %}
{% endtabs %}

### 3.5 Sign Out

Call `Sign Out` with callback:

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

```
[[FTSDKSignIn instance] signOutWithCompleted:^(FTSDKError * _Nullable error) {
            
}];
```

{% endtab %}
{% endtabs %}

### 3.6 Refresh Token

FID employs JWT for authorization, whereby the `accessToken` has a shorter lifespan than the `refreshToken`.

Additionally, FID automatically calls for a refresh token each time the user accesses the app/game and automatically retries when the `accessToken` has expired.

However, when utilizing FID's `accessToken`, it is crucial to take into account its lifespan.

It is advisable to attempt a refresh token call before requesting APIs with FID's `accessToken` to ensure that it has not expired.

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

```objectivec
[[FTSDKSignIn instance] refreshTokenOnSuccess:^(FTSDKAuthentication * _Nonnull auth) {
    // You can requesting APIs with FID's accessToken to ensure that it has not expired.
    // auth.accessToken
    // auth.refreshToken
} onFailure:^(FTSDKError * _Nonnull error) {
    
}];
```

{% endtab %}
{% endtabs %}

### 3.7 Get User Information

Following a successful login, you can obtain user information at any point by simply making a call to:

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

```objectivec
// First param use for turn on or turn off loading
[[FTSDKSignIn instance] getUserInfoWith:NO onSuccess:^(FTSDKUser * _Nonnull user) {
    
} onFailure:^(FTSDKError * _Nonnull error) {
    
}];
```

{% endtab %}
{% endtabs %}

### 3.8 Change User Password

Following a successful login, you can allow user change their password by simply making a call to:

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

```objectivec
[[FTSDKSignIn instance] changePasswordWithCompleted:^{
        
}];
```

{% endtab %}
{% endtabs %}

### 3.9 Auto Login

By default, FID will request auto login everytime user open app, but if you want to disable auto login, you can use setting when config SDK:

```objectivec
// Turn off autologin if need (you need use FTSDK.requestAutoLogin function to check login manual)
FTSDKConfig.autoLogin = NO;
 
// Remember to set config before [FTSDK didFinishLaunching:application with:launchOptions];
[FTSDK didFinishLaunching:application with:launchOptions];
```

Then, you can manual check SDK is authorized with:

```
[FTSDK requestAutoLoginOnUnauthorized:^{
   // onUnauthorized
}];

// If SDK has authorized, callback will fire on FTSDKAuthDelegate#didSignInSuccess
- (void)didSignInSuccess:(FTSDKSignIn *)signIn didSignInFor:(FTSDKUser *)user withMethod:(NSString *)authType {
    
}
```
