[NEW] Support Google OAuth from external browser (#3134)

* Deep linking to the app

* Handle deep linking
This commit is contained in:
Diego Mello 2021-06-07 14:27:15 -03:00 committed by GitHub
parent b2c60e7b53
commit 7e31ac75f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 5 deletions

View File

@ -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' }) => {

View File

@ -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();