Skip to content

Commit

Permalink
[NEW] Support Google OAuth from external browser (#3134)
Browse files Browse the repository at this point in the history
* Deep linking to the app

* Handle deep linking
  • Loading branch information
diegolmello authored Jun 7, 2021
1 parent b2c60e7 commit 7e31ac7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
20 changes: 15 additions & 5 deletions app/containers/LoginServices.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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,
Expand Down Expand Up @@ -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 = () => {
Expand Down Expand Up @@ -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' }) => {
Expand Down
15 changes: 15 additions & 0 deletions app/sagas/deepLinking.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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');
Expand All @@ -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();
Expand Down

0 comments on commit 7e31ac7

Please sign in to comment.