2017-08-07 73 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返回包裹保存“键”的价值承诺。
相关问题