react-native-keychain icon indicating copy to clipboard operation
react-native-keychain copied to clipboard

TypeError: Cannot read property 'setGenericPasswordForOptions' of null

Open agieocean opened this issue 2 years ago • 21 comments

Version: "react-native-keychain": "^8.1.1" "react-native": "0.71.3" Expected behavior: Set token, get token, display token Actual behavior:

 WARN  Possible Unhandled Promise Rejection (id: 0):
TypeError: Cannot read property 'setGenericPasswordForOptions' of null

Relevant code:

const onSubmit = data => {
    let URL = "http://192.168.1.121:8000/api/token/";
    let config = {
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      }
    }

    data = {
      'username': data.UserName,
      'password': data.Password
    }

    axios.post(URL, data, config).then(response => {
      credentialsKey = data.UserName;
      Keychain.setGenericPassword(credentialsKey, response.data.access, { service: credentialsKey }).then(() => {
        Keychain.getGenericPassword({ service: credentialsKey }).then(response => {
          alert(response);
        });
      });
    });
  }

agieocean avatar Mar 18 '23 18:03 agieocean

Did you find a solution to this issue, I have the same problem.

alexmehta avatar Aug 27 '23 05:08 alexmehta

I have a similar problem while trying to use setGenericPassword. Would highly appreciate any help with this if anyone was able to resolve it.

Expected behavior is to be able to store the tokens and retrieve using await Keychain.getGenericPassword();

I however encounter the following error while trying to set:

Cannot read property 'setGenericPasswordForOptions' of null

and

Keychain Error: Cannot read property 'getGenericPasswordForOptions' of null

while trying to get.

const onLogin = async () => {
  setLoading(true);
  axios
    .post(`/login`, {
	email,
	password,
    })
    .then(async (res) => {
	const { accessToken, refreshToken } = res.data;
	setAuthState({ accessToken, refreshToken, authenticated: true });

	Keychain.setGenericPassword(
            'token',
	    JSON.stringify({ accessToken, refreshToken })
	)
	.then((value) => console.log(value))
	.catch((e) => console.error(e));
	})
    .catch((err: AxiosError) => {
	Alert.alert('Login Failed', 'Something went wrong');
    })
    .finally(() => {
	setLoading(false);
    });
};

dannywamuya avatar Sep 12 '23 16:09 dannywamuya

Same

welschmoor avatar Sep 14 '23 15:09 welschmoor

Same error

TranQuyenSinh avatar Nov 05 '23 09:11 TranQuyenSinh

same error. tragic 🥶 "react-native": "0.72.6", "react-native-keychain": "^8.1.2",

ferrariRoma avatar Nov 25 '23 21:11 ferrariRoma

Hey, I managed to solve my problem. It was an oversight, I realized I can't user react-native-keychain in expo. So incase you are using expo, you could opt for SecureStore

dannywamuya avatar Nov 26 '23 07:11 dannywamuya

Hey, I managed to solve my problem. It was an oversight, I realized I can't user react-native-keychain in expo. So incase you are using expo, you could opt for SecureStore

I just read @dannywamuya's comment after I learned expo managed workflow can't use react-native-keychain. and I solved it using SecureStore you mentioned. thank you so much :)

ferrariRoma avatar Nov 26 '23 21:11 ferrariRoma

I had the same issue... I just deleted node_modules and installed it again :)

yarn -> 1.22.09
---
"react-native": "0.72.7
"react-native-keychain": "^8.1.2"

GabrielMerigo avatar Dec 07 '23 16:12 GabrielMerigo

You can try reinstalling pods and verify podfile.lock contains a link to react-native-keychain nodemodule Screenshot 2024-02-18 at 11 13 30 AM

katdivyareddy10 avatar Feb 18 '24 16:02 katdivyareddy10

same issue, non of the solutions above worked

Khalid-Badawi2001 avatar Sep 03 '24 22:09 Khalid-Badawi2001

yea same here, I was using expo and made the switch to secureStore. Would recommend!

micheci avatar Sep 22 '24 16:09 micheci

Had same issue... (for IOS) make sure you add keychain manually in xcode at the 'capabilities' section and run pod install in the ios folder after installing the package. rebuild your project and it should work. you might experience crash in the app after saving the first time, but make sure your app allows and accepts keychain.

brytoz avatar Nov 11 '24 14:11 brytoz

same issue, non of the solutions above worked. Works fine on iOS but not on Android. I am not using expo. Uninstalling the app and re-building did not fix the issue

"react-native": "^0.73.9", "react-native-keychain": "^9.2.2",

bohnerjosh avatar Nov 14 '24 01:11 bohnerjosh

I had the same issue.I uninstalled the app and then build it again.The error got resolved after the new build.

NJneeraj avatar Nov 19 '24 14:11 NJneeraj

Some more context for my issue: I am not using expo. This also does not happen on iOS. When I run my app initially, react native keychain works fine. However, if I close the app and re-open it again, I get this error when calling setGenericPassword and getGenericPassword.

Tried replicating in a bare react native app and was unable to do so.

bohnerjosh avatar Nov 25 '24 01:11 bohnerjosh

i get same error "react-native": "^0.78.0", "react-native-keychain": "^9.2.3",

using reactnative community no expo

mhiqrambg avatar Mar 09 '25 16:03 mhiqrambg

Could someone please create an example repository for this issue?

DorianMazur avatar Mar 09 '25 19:03 DorianMazur

my problem is resolved, react-native-keychain clashes with react-native-gesture-handle. I installed "react-native-gesture-handler": "^2.24.0", and the problem was solved

mhiqrambg avatar Mar 09 '25 20:03 mhiqrambg

In my case, i simply closed the terminal and did npx react-native run-android and it fixed the issue. I guess android studio needed some dependencies

ats1999 avatar Mar 31 '25 10:03 ats1999

So, the issue still there. Using react-native-cli, no Expo, had to manually link the package for android:

  1. added at the end of android/settings.gradle
include ':react-native-keychain'
project(':react-native-keychain').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-keychain/android')
  1. in android/app/build.gradle added to the dependencies
dependencies {
  ...
  implementation project(':react-native-keychain')
}
  1. in the MainApplication.java added to the package's list:
import com.oblador.keychain.KeychainPackage;

override fun getPackages(): List<ReactPackage> =
    PackageList(this).packages.apply {
        // Packages that cannot be autolinked yet can be added manually here, for example:
        // add(MyReactNativePackage())
        add(KeychainPackage())
    }
  1. then run/build application
yarn start
yarn run android

After this the error is resolved.

"react-native": "0.81.4",
"react-native-keychain": "^10.0.0",

blizhinm avatar Oct 15 '25 20:10 blizhinm