Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: biometrics type #15

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.reactnativesimplebiometrics;

import android.app.Activity;
import android.content.pm.PackageManager;
import androidx.annotation.NonNull;

import java.util.concurrent.Executor;
Expand All @@ -16,8 +17,6 @@
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;



@ReactModule(name = SimpleBiometricsModule.NAME)
public class SimpleBiometricsModule extends ReactContextBaseJavaModule {
public static final String NAME = "SimpleBiometrics";
Expand Down Expand Up @@ -96,4 +95,22 @@ public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationRes
);

}

@ReactMethod
public void getBiometryType(Promise promise) {
try {
ReactApplicationContext context = getReactApplicationContext();
PackageManager pm = context.getPackageManager();

if (pm.hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)) {
promise.resolve("Fingerprint");
} else if (pm.hasSystemFeature(PackageManager.FEATURE_FACE)) {
Comment on lines +105 to +107
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add some references to docs explaining how this works?

does this handles cases where the system has both features but the user has only enabled one of them?

promise.resolve("Face");
} else {
promise.resolve("None");
}
} catch (Exception e) {
promise.resolve("Unknown");
}
}
}
11 changes: 10 additions & 1 deletion example/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@ import RNBiometrics from 'react-native-simple-biometrics';

const App = () => {
const [canAuth, setCanAuth] = useState(false);
const [type, setType] = useState('');
const [authenticated, setAuthenticated] = useState(false);

useEffect(() => {
RNBiometrics.canAuthenticate().then(setCanAuth);
}, []);

useEffect(() => {
if (!canAuth) {
setType('Unknown');
return;
}
RNBiometrics.getBiometryType().then(setType);
}, [canAuth]);

const authenticate = useCallback(async () => {
try {
const success = await RNBiometrics.requestBioAuth(
Expand All @@ -40,7 +49,7 @@ const App = () => {
{authenticated ? '🔓' : '🔒'}
</Text>
<Text style={styles.subtitle}>
{authenticated ? '$1,000,000' : '(tap to unlock)'}
{authenticated ? '$1,000,000' : `(tap to unlock using ${type})`}
</Text>
</>
) : (
Expand Down
20 changes: 20 additions & 0 deletions ios/RNSimpleBiometricsSpec.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#import <ReactCommon/ReactTurboModule.h>
#import <ReactCommon/TurboModuleUtils.h>
#import <React/RCTBridgeModule.h>
#import <memory>

namespace facebook {
namespace react {

class JSI_EXPORT NativeSimpleBiometricsSpecJSI : public ObjCTurboModule {
public:
NativeSimpleBiometricsSpecJSI(const ObjCTurboModule::InitParams &params);

// Declare the methods your module exports
void canAuthenticate(jsi::Runtime &rt, std::function<void(jsi::Value)> &&resolve, std::function<void(jsi::Value)> &&reject);
void requestBioAuth(jsi::Runtime &rt, const std::string &title, const std::string &subtitle, std::function<void(jsi::Value)> &&resolve, std::function<void(jsi::Value)> &&reject);
void getBiometryType(jsi::Runtime &rt, std::function<void(jsi::Value)> &&resolve, std::function<void(jsi::Value)> &&reject);
};

}
}
28 changes: 28 additions & 0 deletions ios/SimpleBiometrics.mm
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,34 @@ @implementation SimpleBiometrics

}

RCT_REMAP_METHOD(getBiometryType,
getBiometryTypeWithResolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
LAContext *context = [[LAContext alloc] init];
NSError *la_error = nil;

// Check if the device can evaluate a biometric policy
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&la_error]) {
if (@available(iOS 11.0, *)) {
// iOS 11+ supports biometryType property
if (context.biometryType == LABiometryTypeFaceID) {
resolve(@"FaceID");
} else if (context.biometryType == LABiometryTypeTouchID) {
resolve(@"TouchID");
} else {
resolve(@"None");
}
} else {
// Fallback for iOS versions earlier than 11.0
resolve(@"Unknown");
}
} else {
// Handle the error case where biometric authentication is not available
resolve(@"None");
}
}

// Don't compile this code when we build for the old architecture.
#ifdef RCT_NEW_ARCH_ENABLED
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
Expand Down
16 changes: 8 additions & 8 deletions package.json

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: did you mean to commit these changes?

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-simple-biometrics",
"version": "1.5.2",
"version": "1.8.0",
"description": "test",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down Expand Up @@ -35,34 +35,34 @@
"ios",
"android"
],
"repository": "https://github.com/smallcase/react-native-simple-biometrics",
"repository": "https://github.com/kierancrown/react-native-simple-biometrics",
"author": "smallcase <[email protected]> (https://github.com/smallcase)",
"license": "MIT",
"bugs": {
"url": "https://github.com/smallcase/react-native-simple-biometrics/issues"
"url": "https://github.com/kierancrown/react-native-simple-biometrics/issues"
},
"homepage": "https://github.com/smallcase/react-native-simple-biometrics#readme",
"homepage": "https://github.com/kierancrown/react-native-simple-biometrics#readme",
"publishConfig": {
"registry": "https://registry.npmjs.org/"
},
"devDependencies": {
"@arkweid/lefthook": "^0.7.7",
"@babel/eslint-parser": "^7.18.2",
"@commitlint/config-conventional": "^17.0.2",
"@react-native-community/eslint-config": "^3.0.2",
"@react-native-community/eslint-config": "^3.2.0",
"@release-it/conventional-changelog": "^5.0.0",
"@types/jest": "^28.1.2",
"@types/react": "~17.0.21",
"@types/react-native": "0.68.0",
"@types/react-native": "^0.73.0",
"commitlint": "^17.0.2",
"eslint": "^8.4.1",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.0.0",
"jest": "^28.1.1",
"pod-install": "^0.1.0",
"prettier": "^2.0.5",
"react": "17.0.2",
"react-native": "0.68.2",
"react": "18.3.1",
"react-native": "^0.76.0",
"react-native-builder-bob": "^0.18.3",
"release-it": "^15.0.0",
"typescript": "^4.5.2"
Expand Down
4 changes: 2 additions & 2 deletions react-native-simple-biometrics.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "json"

package = JSON.parse(File.read(File.join(__dir__, "package.json")))
folly_version = '2021.06.28.00-v2'
folly_version = '2024.01.01.00'
folly_compiler_flags = '-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma -Wno-shorten-64-to-32'

Pod::Spec.new do |s|
Expand All @@ -13,7 +13,7 @@ Pod::Spec.new do |s|
s.authors = package["author"]

s.platforms = { :ios => "10.0" }
s.source = { :git => "https://github.com/smallcase/react-native-simple-biometrics.git", :tag => "#{s.version}" }
s.source = { :git => "https://github.com/kierancrown/react-native-simple-biometrics.git", :tag => "#{s.version}" }

s.source_files = "ios/**/*.{h,m,mm}"

Expand Down
10 changes: 10 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,19 @@ const requestBioAuth = (
return RNBiometricsNative.requestBioAuth(promptTitle, promptMessage);
};

/**
* Get the type of biometric authentication available on the device
*/
const getBiometryType = (): Promise<
'Fingerprint' | 'Face' | 'None' | 'Unknown'
> => {
return RNBiometricsNative.getBiometryType();
};

const RNBiometrics = {
requestBioAuth,
canAuthenticate,
getBiometryType,
};

export default RNBiometrics;