[IMPROVEMENT] Authentication via deep linking (#1418)

This commit is contained in:
Diego Mello 2019-11-27 17:52:49 -03:00 committed by GitHub
parent a0dc2bad4a
commit 02c6a674fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 27 deletions

View File

@ -1,9 +1,10 @@
import * as types from './actionsTypes'; import * as types from './actionsTypes';
export function loginRequest(credentials) { export function loginRequest(credentials, logoutOnError) {
return { return {
type: types.LOGIN.REQUEST, type: types.LOGIN.REQUEST,
credentials credentials,
logoutOnError
}; };
} }

View File

@ -14,9 +14,7 @@ import { isIOS, getBundleId } from '../utils/deviceInfo';
import { extractHostname } from '../utils/server'; import { extractHostname } from '../utils/server';
import fetch, { headers } from '../utils/fetch'; import fetch, { headers } from '../utils/fetch';
import { import { setUser, setLoginServices, loginRequest } from '../actions/login';
setUser, setLoginServices, loginRequest, loginFailure, logout
} from '../actions/login';
import { disconnect, connectSuccess, connectRequest } from '../actions/connect'; import { disconnect, connectSuccess, connectRequest } from '../actions/connect';
import { import {
shareSelectServer, shareSetUser shareSelectServer, shareSetUser
@ -163,7 +161,7 @@ const RocketChat = {
stopListener(listener) { stopListener(listener) {
return listener && listener.stop(); return listener && listener.stop();
}, },
connect({ server, user }) { connect({ server, user, logoutOnError = false }) {
return new Promise((resolve) => { return new Promise((resolve) => {
if (!this.sdk || this.sdk.client.host !== server) { if (!this.sdk || this.sdk.client.host !== server) {
database.setActiveDB(server); database.setActiveDB(server);
@ -208,7 +206,7 @@ const RocketChat = {
this.sdk.connect() this.sdk.connect()
.then(() => { .then(() => {
if (user && user.token) { if (user && user.token) {
reduxStore.dispatch(loginRequest({ resume: user.token })); reduxStore.dispatch(loginRequest({ resume: user.token }, logoutOnError));
} }
}) })
.catch((err) => { .catch((err) => {
@ -380,11 +378,6 @@ const RocketChat = {
}; };
return user; return user;
} catch (e) { } catch (e) {
if (e.data && e.data.message && /you've been logged out by the server/i.test(e.data.message)) {
reduxStore.dispatch(logout({ server: this.sdk.client.host }));
} else {
reduxStore.dispatch(loginFailure(e));
}
throw e; throw e;
} }
}, },

View File

@ -53,17 +53,13 @@ const handleOpen = function* handleOpen({ params }) {
// TODO: needs better test // TODO: needs better test
// if deep link is from same server // if deep link is from same server
if (server === host) { if (server === host && user) {
if (user) { const connected = yield select(state => state.server.connected);
const connected = yield select(state => state.server.connected); if (!connected) {
if (!connected) { yield put(selectServerRequest(host));
yield put(selectServerRequest(host)); yield take(types.SERVER.SELECT_SUCCESS);
yield take(types.SERVER.SELECT_SUCCESS);
}
yield navigate({ params });
} else {
yield put(appStart('outside'));
} }
yield navigate({ params });
} else { } else {
// search if deep link's server already exists // search if deep link's server already exists
const serversDB = database.servers; const serversDB = database.servers;
@ -80,13 +76,18 @@ const handleOpen = function* handleOpen({ params }) {
// do nothing? // do nothing?
} }
// if deep link is from a different server // if deep link is from a different server
const result = yield RocketChat.getServerInfo(server); const result = yield RocketChat.getServerInfo(host);
if (!result.success) { if (!result.success) {
return; return;
} }
Navigation.navigate('OnboardingView', { previousServer: server }); Navigation.navigate('OnboardingView', { previousServer: server });
yield delay(1000); yield delay(1000);
EventEmitter.emit('NewServer', { server: host }); EventEmitter.emit('NewServer', { server: host });
if (params.token) {
yield take(types.SERVER.SELECT_SUCCESS);
yield RocketChat.connect({ server: host, user: { token: params.token } });
}
} }
}; };

View File

@ -9,7 +9,9 @@ import 'moment/min/locales';
import * as types from '../actions/actionsTypes'; import * as types from '../actions/actionsTypes';
import { appStart } from '../actions'; import { appStart } from '../actions';
import { serverFinishAdd, selectServerRequest } from '../actions/server'; import { serverFinishAdd, selectServerRequest } from '../actions/server';
import { loginFailure, loginSuccess, setUser } from '../actions/login'; import {
loginFailure, loginSuccess, setUser, logout
} from '../actions/login';
import { roomsRequest } from '../actions/rooms'; import { roomsRequest } from '../actions/rooms';
import { toMomentLocale } from '../utils/moment'; import { toMomentLocale } from '../utils/moment';
import RocketChat from '../lib/rocketchat'; import RocketChat from '../lib/rocketchat';
@ -24,7 +26,7 @@ const loginWithPasswordCall = args => RocketChat.loginWithPassword(args);
const loginCall = args => RocketChat.login(args); const loginCall = args => RocketChat.login(args);
const logoutCall = args => RocketChat.logout(args); const logoutCall = args => RocketChat.logout(args);
const handleLoginRequest = function* handleLoginRequest({ credentials }) { const handleLoginRequest = function* handleLoginRequest({ credentials, logoutOnError = false }) {
try { try {
let result; let result;
if (credentials.resume) { if (credentials.resume) {
@ -34,7 +36,11 @@ const handleLoginRequest = function* handleLoginRequest({ credentials }) {
} }
return yield put(loginSuccess(result)); return yield put(loginSuccess(result));
} catch (error) { } catch (error) {
yield put(loginFailure(error)); if (logoutOnError) {
yield put(logout());
} else {
yield put(loginFailure(error));
}
} }
}; };

View File

@ -87,7 +87,7 @@ const handleSelectServer = function* handleSelectServer({ server, version, fetch
} }
if (user) { if (user) {
yield RocketChat.connect({ server, user }); yield RocketChat.connect({ server, user, logoutOnError: true });
yield put(setUser(user)); yield put(setUser(user));
yield put(actions.appStart('inside')); yield put(actions.appStart('inside'));
} else { } else {