2017-08-07 72 views
1

通過反應應用身份驗證時,我怎麼能實現像remember me功能?我認爲未加密AsyncStorage不是最好的方式,因爲數據是爲用戶開放的。我試過使用realm,但陷入了無法通過Android使用expo來測試應用程序的問題。它說我需要爲android編譯本地代碼並編輯它(在MainApplication.js中添加領域對象創建)。我不想編輯我的項目,但尚未發佈。 instagram和其他RN-apps如何存儲認證數據?什麼是最好的方法?以反應本機存儲私人數據的最佳方式是什麼?

回答

7

什麼是存儲在反應本土私人數據的最佳方式?

我真的建議您使用庫像react-native-keychain存儲在react-native

私人數據您可以使用它像:

// Generic Password, service argument optional 
Keychain 
    .setGenericPassword(username, password) 
    .then(function() { 
    console.log('Credentials saved successfully!'); 
    }); 

// service argument optional 
Keychain 
    .getGenericPassword() 
    .then(function(credentials) { 
    console.log('Credentials successfully loaded for user ' + credentials.username); 
    }).catch(function(error) { 
    console.log('Keychain couldn\'t be accessed! Maybe no value set?', error); 
    }); 

我希望我的回答對您有所幫助

+0

我已經看到了這個包。但安裝它採用原生iOS和Android的代碼,但沒有任何JS API – Herrgott

+0

@Herrgott是的,它使用本地代碼因技術原因 –

+0

我以爲只有在生產中使用本地代碼。但我仍然沒有進行「反應原生升級」。我使用'react-native-sqlite-storage'和我自己的書面CRUD庫部分解決了這個問題 – Herrgott

0

您可以使用簡單的存儲反應本地的保存對鍵=>值,以打字稿隔壁班可以幫助你:

export class MyStorage { 

    handler: Storage; 

    constructor(){ 
     this.handler = require('react-native-local-storage'); 
    } 

    public saveData(key: string, value: string){ 
     this.handler.save(key,value).then(() => { 
      this.handler.get(key).then((data) => {console.log("get: ", data)}); 

      }) 
    } 

    public getData(key: string) : Promise<any> {   
     return this.handler.get(key); 
    } 

} 
  • saveData方法存儲可以通過「鍵」訪問的「值」。
  • 的getData返回包裹保存「鍵」的價值承諾。
相關問題