oblador / react-native-keychain

:key: Keychain Access for React Native
MIT License
3.22k stars 518 forks source link

TypeError: Cannot read property 'setGenericPasswordForOptions' of null #591

Open agieocean opened 1 year ago

agieocean commented 1 year ago

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);
        });
      });
    });
  }
alexmehta commented 1 year ago

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

dannywamuya commented 1 year ago

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);
    });
};
welschmoor commented 1 year ago

Same

TranQuyenSinh commented 1 year ago

Same error

ferrariRoma commented 1 year ago

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

dannywamuya commented 1 year ago

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

ferrariRoma commented 1 year ago

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 :)

GabrielMerigo commented 12 months ago

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"
katdivyareddy10 commented 9 months ago

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
Khalid-Badawi2001 commented 2 months ago

same issue, non of the solutions above worked

micheci commented 2 months ago

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

brytoz commented 3 weeks ago

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.

bohnerjosh commented 2 weeks ago

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",

NJneeraj commented 1 week ago

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

bohnerjosh commented 1 week ago

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.