将Firebase凭证存储/检索到React-native-keychain

时间:2019-05-26 02:43:26

标签: react-native react-native-keychain

需要知道使用react native钥匙串从react-native firebase存储/撤消凭证的正确方法,文档仅显示如何保存用户名和密码,而firebase返回对象

当存储数据时,它返回true 并在检索时返回_40: 0 _55: undefined _65: 1 _72: null.

keychainhelper.js

import * as Keychain from 'react-native-keychain';



const saveUserToken = async (user) =>{

   const credentials = await Keychain.setGenericPassword('userdata',user);
   console.log('credentials', credentials)

}

const retrieveUserToken = async (user) =>{

    try {
        // Retrieve the credentials
        const credentials = await Keychain.getGenericPassword();
        if (credentials) {
          console.log('Credentials successfully loaded for user ' + credentials.user);
        } else {
          console.log('No credentials stored');
        }
      } catch (error) {
        console.log('Keychain couldn\'t be accessed!', error);
      }
    //   await Keychain.resetGenericPassword();

}

App.js

import{ retrieveUserToken ,saveUserToken } from './keychainhelper.js'

login(){
     try{
        await firebase.auth().signInWithEmailAndPassword(email, password)
          .then(res =>{
              let userData = JSON.parse(res.user)
              console.log('userData',userData)
              let store = await saveUserToken(userData)
              console.log('store',store)


             let retrieve = retrieveUserToken()
             console.log('retrieve',collect)

}


}
}

响应

   Object {
       "apiKey": "jkbdsc78wgf8wb98fhg89sgc87dgsc3fddsjhbcds",
       "appName": "[DEFAULT]",
       "authDomain": "myapp-89hg9cd.firebaseapp.com",
       "createdAt": "1531206612000",
       "displayName": null,
       "email": "user393@gmail.com",
       "emailVerified": false,
       ...
       "stsTokenManager": Object {
            "accessToken":"qwerty",
            "apiKey": "qwerty",
            "expirationTime": 1531339781120,
            "refreshToken": "qwerty",
       },
       "uid": "qwerty123",
     }

0 个答案:

没有答案