oblador / react-native-keychain

:key: Keychain Access for React Native
MIT License
3.21k stars 520 forks source link

index.js SECURITY_LEVEL not set safely (with fix) #388

Open donzthefonz opened 4 years ago

donzthefonz commented 4 years ago

Was struggling with some errors like: TypeError: Cannot read property 'SECURITY_LEVEL_ANY' of undefined or null is not an object (evaluating 'RNKeychainManager.SECURITY_LEVEL_ANY') (like https://github.com/oblador/react-native-keychain/issues/221)

The problem is within index.js:

 export const SECURITY_LEVEL = Object.freeze({
    ANY: RNKeychainManager.SECURITY_LEVEL_ANY,
   SECURE_SOFTWARE: RNKeychainManager.SECURITY_LEVEL_SECURE_SOFTWARE,
   SECURE_HARDWARE: RNKeychainManager.SECURITY_LEVEL_SECURE_HARDWARE,
});

It's not safe and should be changed to:

export const SECURITY_LEVEL = Object.freeze({ 
  ANY: RNKeychainManager && RNKeychainManager.SECURITY_LEVEL_ANY, 
  SECURE_SOFTWARE: RNKeychainManager && RNKeychainManager.SECURITY_LEVEL_SECURE_SOFTWARE, 
  SECURE_HARDWARE: RNKeychainManager && RNKeychainManager.SECURITY_LEVEL_SECURE_HARDWARE, });
Relax594 commented 4 years ago

Please provide a PR for that. Im facing the same issue.

omorkved commented 4 years ago

This worked for me (using react-native-pincode which relies on this). Please PR so that other repos that depend on this get the change!

PoulsQ commented 4 years ago

Someone in the project can PR this pls ?

billnbell commented 3 years ago

Please?!?!!

react-native-keychain+6.2.0.patch

diff --git a/node_modules/react-native-keychain/index.js b/node_modules/react-native-keychain/index.js
index e81235e..d412ebe 100644
--- a/node_modules/react-native-keychain/index.js
+++ b/node_modules/react-native-keychain/index.js
@@ -4,9 +4,9 @@ import { NativeModules, Platform } from 'react-native';
 const { RNKeychainManager } = NativeModules;

 export const SECURITY_LEVEL = Object.freeze({
-  ANY: RNKeychainManager.SECURITY_LEVEL_ANY,
-  SECURE_SOFTWARE: RNKeychainManager.SECURITY_LEVEL_SECURE_SOFTWARE,
-  SECURE_HARDWARE: RNKeychainManager.SECURITY_LEVEL_SECURE_HARDWARE,
+  ANY: RNKeychainManager && RNKeychainManager.SECURITY_LEVEL_ANY,
+  SECURE_SOFTWARE: RNKeychainManager && RNKeychainManager.SECURITY_LEVEL_SECURE_SOFTWARE,
+  SECURE_HARDWARE: RNKeychainManager && RNKeychainManager.SECURITY_LEVEL_SECURE_HARDWARE,
 });

 export const ACCESSIBLE = Object.freeze({
hengkx commented 3 years ago

please fix. thanks

PoulsQ commented 3 years ago

Actually I move to https://docs.expo.io/versions/latest/sdk/securestore/ until this is not fixed. :'(

CWolfs commented 3 years ago

Any one of you people asking for a PR could make the PR for this. Take the time to support the library and make this PR. It's a great library and I'm sure it would be helpful :).

braytonstafford commented 3 years ago

Submitted pull request with the suggested fix

Pull request

Pavel-Lunin commented 2 years ago

Was this your error?: null is not an object (evaluating 'RNKeychainManager.SECURITY_LEVEL_ANY')

I got this error and fixed it per https://github.com/oblador/react-native-keychain/issues/388

You need to change a line within react-native-keychain, but it's within the node_modules directory of react-native-pincode which is itself within your own app's node_modules. So it took me a bit of poking around to find it.

change directories into node_modules/\@haskkor/react-native-pincode/node_modules/react-native-keychain/ open index.js with your favorite editor

Replace the follow line, which is contained within export const SECURITY_LEVEL = Object.freeze({...}); : ANY: RNKeychainManager.SECURITY_LEVEL_ANY,

with

ANY: RNKeychainManager && RNKeychainManager.SECURITY_LEVEL_ANY,

and don't change anything else.

It looks like this needs to be PR'd into react-native-keychain so that it becomes a permanent fix. (I believe reinstalling node_modules will undo your local changes).

For development purposes, this short-term fix worked for me.