https://github.com/Cap-go/capacitor-native-biometric
Use biometrics confirm device owner presence or authenticate users.
https://github.com/Cap-go/capacitor-native-biometric
capacitor capacitor-plugin ionic
Last synced: 4 months ago
JSON representation
Use biometrics confirm device owner presence or authenticate users.
- Host: GitHub
- URL: https://github.com/Cap-go/capacitor-native-biometric
- Owner: Cap-go
- License: mit
- Fork: true (epicshaggy/capacitor-native-biometric)
- Created: 2023-05-22T23:40:01.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-02-17T18:35:54.000Z (8 months ago)
- Last Synced: 2025-05-27T08:34:05.286Z (4 months ago)
- Topics: capacitor, capacitor-plugin, ionic
- Language: Java
- Homepage: https://capgo.app
- Size: 6.11 MB
- Stars: 53
- Watchers: 10
- Forks: 20
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- awesome-capacitor - Native Biometric - This plugin gives access to the native biometric apis for android and iOS ([Capgo plugins](https://capgo.app/))
README
➡️ Get Instant updates for your App with Capgo 🚀
Fix your annoying bug now, Hire a Capacitor expert 💪
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 7)
- `npm i @capgo/capacitor-native-biometric`
## Usage
```ts
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.
| Code | Description | Platform |
| ---- | --------------------------- | ---------------------------- |
| 0 | Unknown Error | Android, iOS |
| 1 | Biometrics Unavailable | Android, iOS |
| 2 | User Lockout | Android, iOS |
| 3 | Biometrics Not Enrolled | Android, iOS |
| 4 | User Temporary Lockout | Android (Lockout for 30sec) |
| 10 | Authentication Failed | Android, iOS |
| 11 | App Cancel | iOS |
| 12 | Invalid Context | iOS |
| 13 | Not Interactive | iOS |
| 14 | Passcode Not Set | Android, iOS |
| 15 | System Cancel | Android, iOS |
| 16 | User Cancel | Android, iOS |
| 17 | User Fallback | Android, iOS |* [`isAvailable(...)`](#isavailable)
* [`verifyIdentity(...)`](#verifyidentity)
* [`getCredentials(...)`](#getcredentials)
* [`setCredentials(...)`](#setcredentials)
* [`deleteCredentials(...)`](#deletecredentials)
* [Interfaces](#interfaces)
* [Enums](#enums)### isAvailable(...)
```typescript
isAvailable(options?: IsAvailableOptions | undefined) => Promise
```Checks if biometric authentication hardware is available.
| Param | Type |
| ------------- | ----------------------------------------------------------------- |
| **`options`** |IsAvailableOptions
|**Returns:**
Promise<AvailableResult>
**Since:** 1.0.0
--------------------
### verifyIdentity(...)
```typescript
verifyIdentity(options?: BiometricOptions | undefined) => Promise
```Prompts the user to authenticate with biometrics.
| Param | Type |
| ------------- | ------------------------------------------------------------- |
| **`options`** |BiometricOptions
|**Since:** 1.0.0
--------------------
### getCredentials(...)
```typescript
getCredentials(options: GetCredentialOptions) => Promise
```Gets the stored credentials for a given server.
| Param | Type |
| ------------- | --------------------------------------------------------------------- |
| **`options`** |GetCredentialOptions
|**Returns:**
Promise<Credentials>
**Since:** 1.0.0
--------------------
### setCredentials(...)
```typescript
setCredentials(options: SetCredentialOptions) => Promise
```Stores the given credentials for a given server.
| Param | Type |
| ------------- | --------------------------------------------------------------------- |
| **`options`** |SetCredentialOptions
|**Since:** 1.0.0
--------------------
### deleteCredentials(...)
```typescript
deleteCredentials(options: DeleteCredentialOptions) => Promise
```Deletes the stored credentials for a given server.
| Param | Type |
| ------------- | --------------------------------------------------------------------------- |
| **`options`** |DeleteCredentialOptions
|**Since:** 1.0.0
--------------------
### Interfaces
#### AvailableResult
| Prop | Type |
| ------------------ | ----------------------------------------------------- |
| **`isAvailable`** |boolean
|
| **`biometryType`** |BiometryType
|
| **`errorCode`** |number
|#### IsAvailableOptions
| Prop | Type | Description |
| ----------------- | -------------------- | ----------------------------------------------------------------------------------------------------- |
| **`useFallback`** |boolean
| Specifies if should fallback to passcode authentication if biometric authentication is not available. |#### BiometricOptions
| Prop | Type | Description | Default |
| -------------------------- | --------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------- |
| **`reason`** |string
| | |
| **`title`** |string
| | |
| **`subtitle`** |string
| | |
| **`description`** |string
| | |
| **`negativeButtonText`** |string
| | |
| **`useFallback`** |boolean
| Specifies if should fallback to passcode authentication if biometric authentication fails. | |
| **`fallbackTitle`** |string
| Only 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. | |
| **`maxAttempts`** |number
| Only for Android. Set a maximum number of attempts for biometric authentication. The maximum allowed by android is 5. |1
|
| **`allowedBiometryTypes`** |BiometryType[]
| Only for Android. Specify which biometry types are allowed for authentication. If not specified, all available types will be allowed. | |#### Credentials
| Prop | Type |
| -------------- | ------------------- |
| **`username`** |string
|
| **`password`** |string
|#### GetCredentialOptions
| Prop | Type |
| ------------ | ------------------- |
| **`server`** |string
|#### SetCredentialOptions
| Prop | Type |
| -------------- | ------------------- |
| **`username`** |string
|
| **`password`** |string
|
| **`server`** |string
|#### DeleteCredentialOptions
| Prop | Type |
| ------------ | ------------------- |
| **`server`** |string
|### Enums
#### BiometryType
| Members | Value |
| ------------------------- | -------------- |
| **`NONE`** |0
|
| **`TOUCH_ID`** |1
|
| **`FACE_ID`** |2
|
| **`FINGERPRINT`** |3
|
| **`FACE_AUTHENTICATION`** |4
|
| **`IRIS_AUTHENTICATION`** |5
|
| **`MULTIPLE`** |6
|## Face ID (iOS)
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:
```xml
NSFaceIDUsageDescription
For an easier and faster log in.
```## Biometric (Android)
To use android's BiometricPrompt api you must add the following permission to your AndroidManifest.xml:
```xml
```
And register the plugin by adding it to you MainActivity's onCreate (Not needed for Capacitor 3):
```java
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>() {{
// Additional plugins you've installed go here
// Ex: add(TotallyAwesomePlugin.class);
add(NativeBiometric.class);
}});
}
}
```## Contributors
[Jonthia](https://github.com/jonthia)
[QliQ.dev](https://github.com/qliqdev)
[Brian Weasner](https://github.com/brian-weasner)
[Mohamed Diarra](https://github.com/mohdiarra)
### Want to Contribute?Learn about contributing [HERE](./CONTRIBUTING.md)
## Notes
Hasn't been tested on Android API level 22 or lower.