Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native WebView, how to store username and password

i am creating a very simple app for android and iphone/ipad that uses only webview. How can i store username and password so the user would not have to type them in every single time. Thanks in advance.

import React, { Component } from 'react';
import {
  AppRegistry,
  WebView
} from 'react-native';

export default class myapp extends Component {
  render() {
    return (
      <WebView
        source={{uri: 'https://mysecretappurl.com'}}
      />
    );
  }
}

AppRegistry.registerComponent('myapp', () => myapp);

Thank you Fabian for a quick response. I got it solved with injectedJavascript and the data persist even if i close and relaunch the app both on android and ios. I got stuck as at first as tried to go with asyncstorage and reactnativ-webview-bridge but i failed to implement them due to my lack of knowledge.

import React, { Component } from 'react';
import {
  AppRegistry,
  WebView
} from 'react-native';

export default class myapp extends Component {
  render() {
    let jsCode = `
        var cookie={};
        document.cookie.split('; ').forEach(function(i){cookie[i.split('=')[0]]=i.split('=')[1]});
        document.querySelector('#email').value=cookie['email'] || '';
        document.querySelector('#password').value=cookie['password'] || '';
        document.querySelector('#login button').onclick = function(){
            document.cookie = 'email='+document.querySelector('#email').value;
            document.cookie = 'password='+document.querySelector('#password').value;
        };
    `;
    return (
      <WebView
        source={{uri: 'https://mysecretappurl.com'}}
        injectedJavaScript={jsCode}
      />
    );
  }
}

AppRegistry.registerComponent('myapp', () => myapp);
like image 356
Hendrik Kangro Avatar asked Jan 18 '17 13:01

Hendrik Kangro


1 Answers

I don't know if I understand you correctly: You are writing a "minified browser" with react native only to show your webpage and you want to prefill the login form on that page?

If it's true you are searching for a possibility to exchange data from your React Native app with your page in the WebView component. Take a look at this tutorial of react-native-webview-bridge .

I would try the following:

  • Communicate with your Webpage and establish a listener for your login form to pass the credentials to your RN app
  • Use a module like react-native-simple-store to store the credentials in your RN app
  • If you start the app the next time check your storage and if the credentials are not empty send them to your webpage via bridge/injected javascript
like image 178
Fabian Avatar answered Sep 19 '22 22:09

Fabian