2019-07-29 15:43:53 +02:00
|
|
|
const React = require('react');
|
2019-07-29 15:58:33 +02:00
|
|
|
|
2018-03-09 20:59:12 +00:00
|
|
|
const { View } = require('react-native');
|
2019-07-29 15:58:33 +02:00
|
|
|
const { Button } = require('react-native');
|
2019-07-29 15:43:53 +02:00
|
|
|
const { WebView } = require('react-native-webview');
|
2018-03-09 20:59:12 +00:00
|
|
|
const { connect } = require('react-redux');
|
|
|
|
const { ScreenHeader } = require('lib/components/screen-header.js');
|
|
|
|
const { reg } = require('lib/registry.js');
|
|
|
|
const { _ } = require('lib/locale.js');
|
|
|
|
const { BaseScreenComponent } = require('lib/components/base-screen.js');
|
|
|
|
const parseUri = require('lib/parseUri');
|
2017-07-06 19:29:09 +00:00
|
|
|
|
2017-07-14 18:49:14 +00:00
|
|
|
class OneDriveLoginScreenComponent extends BaseScreenComponent {
|
2019-09-12 22:16:42 +00:00
|
|
|
static navigationOptions() {
|
2017-07-06 19:29:09 +00:00
|
|
|
return { header: null };
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
2018-03-09 20:59:12 +00:00
|
|
|
this.state = { webviewUrl: '' };
|
2017-07-06 19:29:09 +00:00
|
|
|
this.authCode_ = null;
|
|
|
|
}
|
|
|
|
|
2018-04-30 17:38:19 +01:00
|
|
|
UNSAFE_componentWillMount() {
|
2017-07-06 19:29:09 +00:00
|
|
|
this.setState({
|
2017-07-06 19:48:17 +00:00
|
|
|
webviewUrl: this.startUrl(),
|
2017-07-06 19:29:09 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-06 19:48:17 +00:00
|
|
|
startUrl() {
|
2019-07-29 15:43:53 +02:00
|
|
|
return reg
|
|
|
|
.syncTarget()
|
|
|
|
.api()
|
|
|
|
.authCodeUrl(this.redirectUrl());
|
2017-07-06 19:48:17 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 19:29:09 +00:00
|
|
|
redirectUrl() {
|
2019-07-29 15:43:53 +02:00
|
|
|
return reg
|
|
|
|
.syncTarget()
|
|
|
|
.api()
|
|
|
|
.nativeClientRedirectUrl();
|
2017-07-06 19:29:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async webview_load(noIdeaWhatThisIs) {
|
|
|
|
// This is deprecated according to the doc but since the non-deprecated property (source)
|
|
|
|
// doesn't exist, use this for now. The whole component is completely undocumented
|
|
|
|
// at the moment so it's likely to change.
|
|
|
|
const url = noIdeaWhatThisIs.url;
|
2017-11-28 21:49:58 +00:00
|
|
|
const parsedUrl = parseUri(url);
|
2017-07-06 19:29:09 +00:00
|
|
|
|
2017-11-28 21:49:58 +00:00
|
|
|
if (!this.authCode_ && parsedUrl && parsedUrl.queryKey && parsedUrl.queryKey.code) {
|
2019-07-29 15:43:53 +02:00
|
|
|
this.authCode_ = parsedUrl.queryKey.code;
|
2017-07-06 19:29:09 +00:00
|
|
|
|
2017-07-08 00:25:10 +01:00
|
|
|
try {
|
2019-07-29 15:43:53 +02:00
|
|
|
await reg
|
|
|
|
.syncTarget()
|
|
|
|
.api()
|
|
|
|
.execTokenRequest(this.authCode_, this.redirectUrl(), true);
|
2018-03-09 20:59:12 +00:00
|
|
|
this.props.dispatch({ type: 'NAV_BACK' });
|
2017-07-24 22:52:30 +01:00
|
|
|
reg.scheduleSync(0);
|
2017-07-08 00:25:10 +01:00
|
|
|
} catch (error) {
|
2019-09-19 22:51:18 +01:00
|
|
|
alert(`Could not login to OneDrive. Please try again\n\n${error.message}\n\n${url}`);
|
2017-07-08 00:25:10 +01:00
|
|
|
}
|
2017-07-06 19:29:09 +00:00
|
|
|
|
2017-07-06 19:48:17 +00:00
|
|
|
this.authCode_ = null;
|
2017-07-06 19:29:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 23:17:02 +00:00
|
|
|
async webview_error() {
|
|
|
|
alert('Could not load page. Please check your connection and try again.');
|
2017-07-06 19:48:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
retryButton_click() {
|
|
|
|
// It seems the only way it would reload the page is by loading an unrelated
|
|
|
|
// URL, waiting a bit, and then loading the actual URL. There's probably
|
|
|
|
// a better way to do this.
|
|
|
|
|
|
|
|
this.setState({
|
2018-03-09 20:59:12 +00:00
|
|
|
webviewUrl: 'https://microsoft.com',
|
2017-07-06 19:48:17 +00:00
|
|
|
});
|
|
|
|
this.forceUpdate();
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
this.setState({
|
|
|
|
webviewUrl: this.startUrl(),
|
|
|
|
});
|
|
|
|
this.forceUpdate();
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
|
2017-07-06 19:29:09 +00:00
|
|
|
render() {
|
|
|
|
const source = {
|
|
|
|
uri: this.state.webviewUrl,
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2017-07-06 19:29:09 +00:00
|
|
|
|
|
|
|
return (
|
2017-07-14 18:49:14 +00:00
|
|
|
<View style={this.styles().screen}>
|
2019-07-29 15:43:53 +02:00
|
|
|
<ScreenHeader title={_('Login with OneDrive')} />
|
2017-07-06 19:29:09 +00:00
|
|
|
<WebView
|
|
|
|
source={source}
|
2020-05-21 09:14:33 +01:00
|
|
|
onNavigationStateChange={o => {
|
2019-07-29 15:43:53 +02:00
|
|
|
this.webview_load(o);
|
|
|
|
}}
|
|
|
|
onError={() => {
|
|
|
|
this.webview_error();
|
|
|
|
}}
|
2017-07-06 19:29:09 +00:00
|
|
|
/>
|
2019-07-29 15:43:53 +02:00
|
|
|
<Button
|
|
|
|
title={_('Refresh')}
|
|
|
|
onPress={() => {
|
|
|
|
this.retryButton_click();
|
|
|
|
}}
|
|
|
|
></Button>
|
2017-07-06 19:29:09 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-12 22:16:42 +00:00
|
|
|
const OneDriveLoginScreen = connect(() => {
|
2019-07-29 15:43:53 +02:00
|
|
|
return {};
|
|
|
|
})(OneDriveLoginScreenComponent);
|
2017-07-06 19:29:09 +00:00
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
module.exports = { OneDriveLoginScreen };
|