diff --git a/app/containers/LoginServices.js b/app/containers/LoginServices.js index 4ae647b5..11d5dd57 100644 --- a/app/containers/LoginServices.js +++ b/app/containers/LoginServices.js @@ -1,6 +1,6 @@ import React from 'react'; import { - View, StyleSheet, Text, Animated, Easing + View, StyleSheet, Text, Animated, Easing, Linking } from 'react-native'; import PropTypes from 'prop-types'; import { connect } from 'react-redux'; @@ -24,6 +24,9 @@ const SERVICE_HEIGHT = 58; const BORDER_RADIUS = 2; const SERVICES_COLLAPSED_HEIGHT = 174; +const LOGIN_STYPE_POPUP = 'popup'; +const LOGIN_STYPE_REDIRECT = 'redirect'; + const styles = StyleSheet.create({ serviceButton: { borderRadius: BORDER_RADIUS, @@ -122,9 +125,9 @@ class LoginServices extends React.PureComponent { const endpoint = 'https://accounts.google.com/o/oauth2/auth'; const redirect_uri = `${ server }/_oauth/google?close`; const scope = 'email'; - const state = this.getOAuthState(); + const state = this.getOAuthState(LOGIN_STYPE_REDIRECT); const params = `?client_id=${ clientId }&redirect_uri=${ redirect_uri }&scope=${ scope }&state=${ state }&response_type=code`; - this.openOAuth({ url: `${ endpoint }${ params }` }); + Linking.openURL(`${ endpoint }${ params }`); } onPressLinkedin = () => { @@ -219,9 +222,16 @@ class LoginServices extends React.PureComponent { } } - getOAuthState = () => { + getOAuthState = (loginStyle = LOGIN_STYPE_POPUP) => { const credentialToken = random(43); - return Base64.encodeURI(JSON.stringify({ loginStyle: 'popup', credentialToken, isCordova: true })); + let obj = { loginStyle, credentialToken, isCordova: true }; + if (loginStyle === LOGIN_STYPE_REDIRECT) { + obj = { + ...obj, + redirectUrl: 'rocketchat://auth' + }; + } + return Base64.encodeURI(JSON.stringify(obj)); } openOAuth = ({ url, ssoToken, authType = 'oauth' }) => { diff --git a/app/sagas/deepLinking.js b/app/sagas/deepLinking.js index 985a2855..556573a5 100644 --- a/app/sagas/deepLinking.js +++ b/app/sagas/deepLinking.js @@ -16,6 +16,7 @@ import { import { localAuthenticate } from '../utils/localAuthentication'; import { goRoom } from '../utils/goRoom'; import { loginRequest } from '../actions/login'; +import log from '../utils/log'; const roomTypes = { channel: 'c', direct: 'd', group: 'p', channels: 'l' @@ -93,6 +94,15 @@ const fallbackNavigation = function* fallbackNavigation() { yield put(appInit()); }; +const handleOAuth = function* handleOAuth({ params }) { + const { credentialToken, credentialSecret } = params; + try { + yield RocketChat.loginOAuthOrSso({ oauth: { credentialToken, credentialSecret } }); + } catch (e) { + log(e); + } +}; + const handleOpen = function* handleOpen({ params }) { const serversDB = database.servers; const serversCollection = serversDB.get('servers'); @@ -108,6 +118,11 @@ const handleOpen = function* handleOpen({ params }) { }); } + if (params.type === 'oauth') { + yield handleOAuth({ params }); + return; + } + // If there's no host on the deep link params and the app is opened, just call appInit() if (!host) { yield fallbackNavigation();