BlueWallet/screen/wallets/hodlHodlLogin.js

65 lines
1.8 KiB
JavaScript
Raw Normal View History

2020-07-15 19:32:59 +02:00
import React, { useRef } from 'react';
2020-06-15 20:47:54 +02:00
import { WebView } from 'react-native-webview';
2020-07-15 19:32:59 +02:00
import { useRoute, useNavigation } from '@react-navigation/native';
2020-12-25 17:09:53 +01:00
import { SafeBlueArea } from '../../BlueComponents';
import navigationStyle from '../../components/navigationStyle';
2020-07-20 15:38:46 +02:00
import loc from '../../loc';
2020-06-15 20:47:54 +02:00
const url = 'https://accounts.hodlhodl.com/accounts/request_access?attributes=api_key,api_signature_key';
let lastTimeIvebeenHere = 0;
const INJECTED_JAVASCRIPT = `(function() {
window.postMessage = function (data) {
2020-07-20 15:38:46 +02:00
window.ReactNativeWebView && window.ReactNativeWebView.postMessage(data);
2020-06-15 20:47:54 +02:00
}
2020-07-20 15:38:46 +02:00
2020-06-15 20:47:54 +02:00
})();`;
2020-07-15 19:32:59 +02:00
const HodlHodlLogin = () => {
const webView = useRef();
const { cb } = useRoute().params;
const navigation = useNavigation();
return (
<SafeBlueArea>
<WebView
2020-07-31 19:01:04 +02:00
incognito
2020-07-15 19:32:59 +02:00
injectedJavaScript={INJECTED_JAVASCRIPT}
ref={webView}
source={{ uri: url }}
onMessage={e => {
// this is a handler which receives messages sent from within the browser
if (lastTimeIvebeenHere && +new Date() - lastTimeIvebeenHere < 5000) return;
lastTimeIvebeenHere = +new Date();
// page can post messages several times, and that can confuse our react navigation, so we have protection
// against that
let json = false;
try {
json = JSON.parse(e.nativeEvent.data);
} catch (_) {}
if (json && json.allowed && json.data && json.data.api_key) {
cb(json.data.api_key, json.data.api_signature_key);
navigation.dangerouslyGetParent().pop();
}
}}
/>
</SafeBlueArea>
);
};
2020-06-15 20:47:54 +02:00
2021-02-15 09:03:54 +01:00
HodlHodlLogin.navigationOptions = navigationStyle(
{
closeButton: true,
headerLeft: null,
},
opts => ({ ...opts, title: loc.hodl.login }),
);
2020-06-15 20:47:54 +02:00
2020-07-15 19:32:59 +02:00
export default HodlHodlLogin;