# Face Authentication

To perform a face authentication it is required to have already an "Identification" and its verification ID. The process consist in a liveness check of the user and a final check to verify the match with the identification.

## Implementation

* Make your class conform to `AuthenteqFaceAuthenticationDelegate` protocol
* Use your client keys to create a `UIViewController` from Authenteq SDK
* Present returned `UIViewController` modally
* Handle result in your delegate implementation

## Example to Start a Face Authentication

Please update the below code with your Client ID and Client Secret from your [Customer Dashboard](https://customer-dashboard.app.authenteq.com/sign-in).&#x20;

{% hint style="warning" %}
We suggest to use the token authentication in production environment.
{% endhint %}

We suggest to app the following code to the relevant Storyboard or ViewController.

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

```swift
import AuthenteqFlow

class ExampleViewController: UIViewController {

  func faceAuthentication() {
    let faceAuthenticationParams = FaceAuthenticationParams(
      clientId: "< YOUR CLIENT ID >",
      clientSecret: "< YOUR CLIENT SECRET >",
      verificationId: verificationId
    )
    let viewController = AuthenteqFlow.instance.faceAuthenticationViewController(
      with: faceAuthenticationParams, 
      delegate: self
    )
    viewController.modalPresentationStyle = .fullScreen
    present(viewController, animated: true)
  }
}  
  
extension ExampleViewController: AuthenteqFaceAuthenticationDelegate {

  func authenteqDidFinishFaceAuthentication(with code: String) {
    presentedViewController?.dismiss(animated: true)
    // Check face authentication result with the CODE provided
  }

  func authenteqDidFailFaceAuthentication(with error: AuthenteqFlowError) {
    // Handle error
    presentedViewController?.dismiss(animated: true)
  }
}
```

{% endtab %}

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

```objectivec
@import AuthenteqFlow;

@interface ExampleViewController: UIViewController <AuthenteqFaceAuthenticationDelegate>
@end

@implementation ExampleViewController

- (void)faceAuthentication {
  FaceAuthenticationParams *params = 
  [[FaceAuthenticationParams alloc] initWithClientId:@"<YOUR CLIENT ID>"
                                        clientSecret:@"<YOUR CLIENT SECRET>"
                                      verificationId:_verificationId];
  
  UIViewController *viewController = 
  [[AuthenteqFlow instance] faceAuthenticationViewControllerWith:params
                                                        delegate:self];
  
  viewController.modalPresentationStyle = UIModalPresentationFullScreen;
  [self presentViewController:viewController animated:true completion:nil];
}

#pragma MARK - AuthenteqFaceAuthenticationDelegate

- (void)authenteqDidFinishFaceAuthenticationWith:(nonnull NSString *)code {
  [self.presentedViewController dismissViewControllerAnimated:true completion:nil];
  NSLog(@"Did finish face authentication with code:\n%@", code);
  // Check face authentication result with the CODE provided
}

- (void)authenteqDidFailFaceAuthenticationWith:(enum AuthenteqFlowError)error {
  // Handle error
  [self.presentedViewController dismissViewControllerAnimated:true completion:nil];
  NSLog(@"Did finish face authentication with error: %ld", (long) error);
}

@end
```

{% endtab %}
{% endtabs %}

Authentication Token can be obtained with face authentication [API](https://docs.authenteq.com/authentication-token#obtain-a-mobile-sdk-temporary-authentication-token-for-face-authentication) and then specified with the `FaceAuthenticationParams` as the following code:

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

```swift
let faceAuthenticationParams = FaceAuthenticationParams(
      clientId: "< YOUR CLIENT ID >",
      token: "< TOKEN >",
      verificationId: verificationId
)
let viewController = AuthenteqFlow.instance.faceAuthenticationViewController(
      with: faceAuthenticationParams, 
      delegate: self
)
present(viewController, animated: true)
```

{% endtab %}

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

```objectivec
FaceAuthenticationParams *params = 
  [[FaceAuthenticationParams alloc] initWithClientId:@"< YOUR CLIENT ID >"
                                               token:@"< TOKEN >"
                                      verificationId:_verificationId];
  
UIViewController *viewController = 
  [[AuthenteqFlow instance] faceAuthenticationViewControllerWith:params
                                                        delegate:self];
                                                        
[self presentViewController:viewController animated:true completion:nil];
```

{% endtab %}
{% endtabs %}

{% hint style="warning" %}
Authentication token can only be used for a single operation. Always generate a new token before start a new face authentication.
{% endhint %}

### Get Face Authentication Result

With the code obtained from `authenteqDidFinishFaceAuthentication` it is possible to get the face authentication result using the following API:

## Obtain the result of a face authentication

<mark style="color:blue;">`GET`</mark> `https://api.app.authenteq.com/mobile-sdk/face-authentication-result`

This endpoint is authorized with Basic Authorization. You should use your `Client ID` and `Client Secret` from the Customer Dashboard as the credentials.

#### Query Parameters

| Name                                   | Type   | Description                                                    |
| -------------------------------------- | ------ | -------------------------------------------------------------- |
| code<mark style="color:red;">\*</mark> | string | The code provided at the conclusion of SDK face authentication |

#### Headers

| Name                                            | Type   | Description                                                                                         |
| ----------------------------------------------- | ------ | --------------------------------------------------------------------------------------------------- |
| Authorization<mark style="color:red;">\*</mark> | string | Your client credentials combined with a colon separator, base64-encoded and prefixed with "Basic ". |

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

```
{
  "accessToken": "81e4cbce-cdad-11eb-8fc3-784f4385af2b"
}
```

{% endtab %}

{% tab title="401  Full authentication is required to perform document recognition." %}

```
{
  "errorCode": "API_KEYS_MISSING",
  "errorMessage": "No API Keys in the Authorization header"
```

{% endtab %}

{% tab title="403 The authenticated customer is disabled and cannot get a token." %}

```
{
  "errorCode": "ACCOUNT_DEACTIVATED",
  "errorMessage": "Account deactivated. Please contact your Authenteq Sales Representative in order to keep using this service"
}huiHjio
```

{% endtab %}
{% endtabs %}

When successful the response will contain a single boolean property named `success`:

```json
{
    "success": true
}
```
