2018-03-09 17:49:35 +00:00
|
|
|
const React = require("react");
|
|
|
|
const Component = React.Component;
|
|
|
|
const { View } = require("react-native");
|
|
|
|
const { WebView, Button, Text } = require("react-native");
|
|
|
|
const { connect } = require("react-redux");
|
|
|
|
const { Log } = require("lib/log.js");
|
|
|
|
const Setting = require("lib/models/Setting.js");
|
|
|
|
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 {
|
2017-07-06 19:29:09 +00:00
|
|
|
static navigationOptions(options) {
|
|
|
|
return { header: null };
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
2018-03-09 17:49:35 +00:00
|
|
|
this.state = { webviewUrl: "" };
|
2017-07-06 19:29:09 +00:00
|
|
|
this.authCode_ = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillMount() {
|
|
|
|
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() {
|
2018-03-09 17:49:35 +00: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() {
|
2018-03-09 17:49:35 +00: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) {
|
2018-03-09 17:49:35 +00:00
|
|
|
Log.info("URL: ", url, parsedUrl.queryKey);
|
2017-07-06 19:29:09 +00:00
|
|
|
|
2018-03-09 17:49:35 +00:00
|
|
|
this.authCode_ = parsedUrl.queryKey.code;
|
2017-07-06 19:29:09 +00:00
|
|
|
|
2017-07-08 00:25:10 +01:00
|
|
|
try {
|
2018-03-09 17:49:35 +00:00
|
|
|
await reg
|
|
|
|
.syncTarget()
|
|
|
|
.api()
|
|
|
|
.execTokenRequest(this.authCode_, this.redirectUrl(), true);
|
|
|
|
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) {
|
2018-03-09 17:49:35 +00: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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-06 19:48:17 +00:00
|
|
|
async webview_error(error) {
|
|
|
|
Log.error(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 17:49:35 +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,
|
2018-03-09 17:49:35 +00:00
|
|
|
};
|
2017-07-06 19:29:09 +00:00
|
|
|
|
|
|
|
return (
|
2017-07-14 18:49:14 +00:00
|
|
|
<View style={this.styles().screen}>
|
2018-03-09 17:49:35 +00:00
|
|
|
<ScreenHeader title={_("Login with OneDrive")} />
|
2017-07-06 19:29:09 +00:00
|
|
|
<WebView
|
|
|
|
source={source}
|
2018-03-09 17:49:35 +00:00
|
|
|
onNavigationStateChange={o => {
|
|
|
|
this.webview_load(o);
|
|
|
|
}}
|
|
|
|
onError={error => {
|
|
|
|
this.webview_error(error);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
title={_("Refresh")}
|
|
|
|
onPress={() => {
|
|
|
|
this.retryButton_click();
|
|
|
|
}}
|
2017-07-06 19:29:09 +00:00
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-09 17:49:35 +00:00
|
|
|
const OneDriveLoginScreen = connect(state => {
|
|
|
|
return {};
|
|
|
|
})(OneDriveLoginScreenComponent);
|
2017-07-06 19:29:09 +00:00
|
|
|
|
2018-03-09 17:49:35 +00:00
|
|
|
module.exports = { OneDriveLoginScreen };
|