Fix SSO login (#754)
* Fix SSO login * Update style * Fix stripTrailingSlashes
This commit is contained in:
parent
408d184dea
commit
da3a0a22b2
5 changed files with 80 additions and 19 deletions
|
|
@ -118,14 +118,15 @@ class SelectServer extends PureComponent {
|
|||
|
||||
onClick = async () => {
|
||||
const preUrl = urlParse(this.props.serverUrl, true);
|
||||
const url = preUrl.protocol + '//' + preUrl.host;
|
||||
const url = stripTrailingSlashes(preUrl.protocol + '//' + preUrl.host);
|
||||
let error = null;
|
||||
|
||||
Keyboard.dismiss();
|
||||
|
||||
if (isValidUrl(url)) {
|
||||
Client4.setUrl(stripTrailingSlashes(url));
|
||||
Client.setUrl(stripTrailingSlashes(url));
|
||||
Client4.setUrl(url);
|
||||
Client.setUrl(url);
|
||||
this.props.actions.handleServerUrlChanged(url);
|
||||
await this.props.actions.getPing();
|
||||
} else {
|
||||
error = {
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@ import PropTypes from 'prop-types';
|
|||
import {injectIntl, intlShape} from 'react-intl';
|
||||
import {
|
||||
InteractionManager,
|
||||
Text,
|
||||
StyleSheet,
|
||||
View,
|
||||
WebView
|
||||
} from 'react-native';
|
||||
|
|
@ -17,6 +19,9 @@ import {ViewTypes} from 'app/constants';
|
|||
import Loading from 'app/components/loading';
|
||||
import StatusBar from 'app/components/status_bar';
|
||||
import PushNotifications from 'app/push_notifications';
|
||||
import {changeOpacity, makeStyleSheetFromTheme} from 'app/utils/theme';
|
||||
|
||||
const jsCode = 'window.postMessage(document.body.innerText)';
|
||||
|
||||
class SSO extends PureComponent {
|
||||
static propTypes = {
|
||||
|
|
@ -35,6 +40,11 @@ class SSO extends PureComponent {
|
|||
constructor(props) {
|
||||
super(props);
|
||||
|
||||
this.state = {
|
||||
error: null,
|
||||
renderWebView: false
|
||||
};
|
||||
|
||||
switch (props.ssoType) {
|
||||
case ViewTypes.GITLAB:
|
||||
this.loginUrl = `${props.serverUrl}/oauth/gitlab/mobile_login`;
|
||||
|
|
@ -49,7 +59,7 @@ class SSO extends PureComponent {
|
|||
|
||||
componentDidMount() {
|
||||
InteractionManager.runAfterInteractions(() => {
|
||||
this.setState({renderWebview: true});
|
||||
this.setState({renderWebView: true});
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -85,6 +95,25 @@ class SSO extends PureComponent {
|
|||
});
|
||||
};
|
||||
|
||||
onMessage = (event) => {
|
||||
try {
|
||||
const response = JSON.parse(event.nativeEvent.data);
|
||||
if (response) {
|
||||
const {
|
||||
id,
|
||||
message,
|
||||
request_id: rId,
|
||||
status_code: statusCode
|
||||
} = response;
|
||||
if (rId && id && message && statusCode !== 200) {
|
||||
this.setState({error: message});
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
// do nothing
|
||||
}
|
||||
};
|
||||
|
||||
onNavigationStateChange = (navState) => {
|
||||
const {url} = navState;
|
||||
|
||||
|
|
@ -93,7 +122,7 @@ class SSO extends PureComponent {
|
|||
const token = res.MMAUTHTOKEN;
|
||||
|
||||
if (token) {
|
||||
this.setState({renderWebview: false});
|
||||
this.setState({renderWebView: false});
|
||||
const {
|
||||
getSession,
|
||||
handleSuccessfulLogin,
|
||||
|
|
@ -111,18 +140,23 @@ class SSO extends PureComponent {
|
|||
};
|
||||
|
||||
render() {
|
||||
if (!this.state || !this.state.renderWebview) {
|
||||
return (
|
||||
<View style={{flex: 1}}>
|
||||
<StatusBar/>
|
||||
<Loading/>
|
||||
const {theme} = this.props;
|
||||
const {error, renderWebView} = this.state;
|
||||
const style = getStyleSheet(theme);
|
||||
|
||||
let content;
|
||||
if (!renderWebView) {
|
||||
content = (
|
||||
<Loading/>
|
||||
);
|
||||
} else if (error) {
|
||||
content = (
|
||||
<View style={style.errorContainer}>
|
||||
<Text style={style.errorText}>{error}</Text>
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<View style={{flex: 1}}>
|
||||
<StatusBar/>
|
||||
} else {
|
||||
content = (
|
||||
<WebView
|
||||
source={{uri: this.loginUrl}}
|
||||
javaScriptEnabledAndroid={true}
|
||||
|
|
@ -132,10 +166,36 @@ class SSO extends PureComponent {
|
|||
onNavigationStateChange={this.onNavigationStateChange}
|
||||
onShouldStartLoadWithRequest={() => true}
|
||||
renderLoading={() => (<Loading/>)}
|
||||
onMessage={this.onMessage}
|
||||
injectedJavaScript={jsCode}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<View style={{flex: 1}}>
|
||||
<StatusBar/>
|
||||
{content}
|
||||
</View>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const getStyleSheet = makeStyleSheetFromTheme((theme) => {
|
||||
return StyleSheet.create({
|
||||
errorContainer: {
|
||||
alignItems: 'center',
|
||||
flex: 1,
|
||||
marginTop: 40
|
||||
},
|
||||
errorText: {
|
||||
color: changeOpacity(theme.centerChannelColor, 0.4),
|
||||
fontSize: 16,
|
||||
fontWeight: '400',
|
||||
lineHeight: 23,
|
||||
paddingHorizontal: 30
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
export default injectIntl(SSO);
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ export function isValidUrl(url = '') {
|
|||
}
|
||||
|
||||
export function stripTrailingSlashes(url = '') {
|
||||
return url.replace(/\/+$/, '').trim();
|
||||
return url.trim().replace(/\/+$/, '');
|
||||
}
|
||||
|
||||
export function removeProtocol(url = '') {
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@
|
|||
"commonmark-react-renderer": "hmhealey/commonmark-react-renderer#11bf3a089900eda6f3e951f8b8c80663cff4d6a3",
|
||||
"deep-equal": "1.0.1",
|
||||
"intl": "1.2.5",
|
||||
"mattermost-redux": "mattermost/mattermost-redux#master",
|
||||
"mattermost-redux": "mattermost/mattermost-redux#rn-release-1.0.1",
|
||||
"prop-types": "15.5.10",
|
||||
"react": "16.0.0-alpha.6",
|
||||
"react-intl": "2.3.0",
|
||||
|
|
|
|||
|
|
@ -3643,9 +3643,9 @@ makeerror@1.0.x:
|
|||
dependencies:
|
||||
tmpl "1.0.x"
|
||||
|
||||
mattermost-redux@mattermost/mattermost-redux#master:
|
||||
mattermost-redux@mattermost/mattermost-redux#rn-release-1.0.1:
|
||||
version "0.0.1"
|
||||
resolved "https://codeload.github.com/mattermost/mattermost-redux/tar.gz/26b897a943a2095bdc793f28c10415441859ad6f"
|
||||
resolved "https://codeload.github.com/mattermost/mattermost-redux/tar.gz/af1095f4b6bbeec4004daa2fe879472de90d85e0"
|
||||
dependencies:
|
||||
deep-equal "1.0.1"
|
||||
harmony-reflect "1.5.1"
|
||||
|
|
|
|||
Loading…
Reference in a new issue