BlueWallet/screen/wallets/hodlHodlLogin.js

60 lines
1.7 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';
import { BlueNavigationStyle, SafeBlueArea } from '../../BlueComponents';
2020-07-15 19:32:59 +02:00
import { useRoute, useNavigation } from '@react-navigation/native';
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
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
2020-07-15 19:32:59 +02:00
HodlHodlLogin.navigationOptions = ({ navigation }) => ({
...BlueNavigationStyle(navigation, true),
2020-07-20 15:38:46 +02:00
title: loc.hodl.login,
2020-07-15 19:32:59 +02:00
headerLeft: null,
});
2020-06-15 20:47:54 +02:00
2020-07-15 19:32:59 +02:00
export default HodlHodlLogin;