6.0.1 • Published 3 days ago

@capgo/capacitor-native-biometric v6.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 days ago

Capacitor Native Biometric

Use biometrics confirm device owner presence or authenticate users. A couple of methods are provided to handle user credentials. These are securely stored using Keychain (iOS) and Keystore (Android).

Installation (Only supports Capacitor 6)

  • npm i @capgo/capacitor-native-biometric

Usage

import { NativeBiometric, BiometryType } from "@capgo/capacitor-native-biometric";

async performBiometricVerification(){
  const result = await NativeBiometric.isAvailable();

  if(!result.isAvailable) return;

  const isFaceID = result.biometryType == BiometryType.FACE_ID;

  const verified = await NativeBiometric.verifyIdentity({
    reason: "For easy log in",
    title: "Log in",
    subtitle: "Maybe add subtitle here?",
    description: "Maybe a description too?",
  })
    .then(() => true)
    .catch(() => false);

  if(!verified) return;

  const credentials = await NativeBiometric.getCredentials({
    server: "www.example.com",
  });
}

// Save user's credentials
NativeBiometric.setCredentials({
  username: "username",
  password: "password",
  server: "www.example.com",
}).then();

// Delete user's credentials
NativeBiometric.deleteCredentials({
  server: "www.example.com",
}).then();

Biometric Auth Errors

This is a plugin specific list of error codes that can be thrown on verifyIdentity failure, or set as a part of isAvailable. It consolidates Android and iOS specific Authentication Error codes into one combined error list.

CodeDescriptionPlatform
0Unknown ErrorAndroid, iOS
1Biometrics UnavailableAndroid, iOS
2User LockoutAndroid, iOS
3Biometrics Not EnrolledAndroid, iOS
4User Temporary LockoutAndroid (Lockout for 30sec)
10Authentication FailedAndroid, iOS
11App CanceliOS
12Invalid ContextiOS
13Not InteractiveiOS
14Passcode Not SetAndroid, iOS
15System CancelAndroid, iOS
16User CancelAndroid, iOS
17User FallbackAndroid, iOS

isAvailable(...)

isAvailable(options?: IsAvailableOptions | undefined) => any

Checks if biometric authentication hardware is available.

ParamType
optionsIsAvailableOptions

Returns: any

Since: 1.0.0


verifyIdentity(...)

verifyIdentity(options?: BiometricOptions | undefined) => any

Prompts the user to authenticate with biometrics.

ParamType
optionsBiometricOptions

Returns: any

Since: 1.0.0


getCredentials(...)

getCredentials(options: GetCredentialOptions) => any

Gets the stored credentials for a given server.

ParamType
optionsGetCredentialOptions

Returns: any

Since: 1.0.0


setCredentials(...)

setCredentials(options: SetCredentialOptions) => any

Stores the given credentials for a given server.

ParamType
optionsSetCredentialOptions

Returns: any

Since: 1.0.0


deleteCredentials(...)

deleteCredentials(options: DeleteCredentialOptions) => any

Deletes the stored credentials for a given server.

ParamType
optionsDeleteCredentialOptions

Returns: any

Since: 1.0.0


Interfaces

IsAvailableOptions

PropTypeDescription
useFallbackbooleanSpecifies if should fallback to passcode authentication if biometric authentication is not available.

AvailableResult

PropType
isAvailableboolean
biometryTypeBiometryType
errorCodenumber

BiometricOptions

PropTypeDescriptionDefault
reasonstring
titlestring
subtitlestring
descriptionstring
negativeButtonTextstring
useFallbackbooleanSpecifies if should fallback to passcode authentication if biometric authentication fails.
fallbackTitlestringOnly for iOS. Set the text for the fallback button in the authentication dialog. If this property is not specified, the default text is set by the system.
maxAttemptsnumberOnly for Android. Set a maximum number of attempts for biometric authentication. The maximum allowed by android is 5.1

GetCredentialOptions

PropType
serverstring

Credentials

PropType
usernamestring
passwordstring

SetCredentialOptions

PropType
usernamestring
passwordstring
serverstring

DeleteCredentialOptions

PropType
serverstring

Enums

BiometryType

MembersValue
NONE0
TOUCH_ID1
FACE_ID2
FINGERPRINT3
FACE_AUTHENTICATION4
IRIS_AUTHENTICATION5
MULTIPLE6

To use FaceID Make sure to provide a value for NSFaceIDUsageDescription, otherwise your app may crash on iOS devices with FaceID.

This value is just the reason for using FaceID. You can add something like the following example to App/info.plist:

<key>NSFaceIDUsageDescription</key>
<string>For an easier and faster log in.</string>

Biometric (Android)

To use android's BiometricPrompt api you must add the following permission to your AndroidManifest.xml:

<uses-permission android:name="android.permission.USE_BIOMETRIC">

And register the plugin by adding it to you MainActivity's onCreate (Not needed for Capacitor 3):

import ee.forgr.biometric.NativeBiometric;

public class MainActivity extends BridgeActivity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Initializes the Bridge
    this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
      // Additional plugins you've installed go here
      // Ex: add(TotallyAwesomePlugin.class);
      add(NativeBiometric.class);
    }});
  }
}

Contributors

Jonthia One Click Web Studio Brian Weasner Mohamed Diarra

Want to Contribute?

Learn about contributing HERE

Notes

Hasn't been tested on Android API level 22 or lower.

6.0.1

3 days ago

6.0.0

11 days ago

5.1.1

17 days ago

5.1.0

8 months ago

5.0.1

12 months ago

5.0.0

12 months ago