[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';
export function loginRequest(credentials) {
export function loginRequest(credentials, logoutOnError) {
return {
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 fetch, { headers } from '../utils/fetch';
import {
setUser, setLoginServices, loginRequest, loginFailure, logout
} from '../actions/login';
import { setUser, setLoginServices, loginRequest } from '../actions/login';
import { disconnect, connectSuccess, connectRequest } from '../actions/connect';
import {
shareSelectServer, shareSetUser
@ -163,7 +161,7 @@ const RocketChat = {
stopListener(listener) {
return listener && listener.stop();
},
connect({ server, user }) {
connect({ server, user, logoutOnError = false }) {
return new Promise((resolve) => {
if (!this.sdk || this.sdk.client.host !== server) {
database.setActiveDB(server);
@ -208,7 +206,7 @@ const RocketChat = {
this.sdk.connect()
.then(() => {
if (user && user.token) {
reduxStore.dispatch(loginRequest({ resume: user.token }));
reduxStore.dispatch(loginRequest({ resume: user.token }, logoutOnError));
}
})
.catch((err) => {
@ -380,11 +378,6 @@ const RocketChat = {
};
return user;
} 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;
}
},

View File

@ -53,17 +53,13 @@ const handleOpen = function* handleOpen({ params }) {
// TODO: needs better test
// if deep link is from same server
if (server === host) {
if (user) {
const connected = yield select(state => state.server.connected);
if (!connected) {
yield put(selectServerRequest(host));
yield take(types.SERVER.SELECT_SUCCESS);
}
yield navigate({ params });
} else {
yield put(appStart('outside'));
if (server === host && user) {
const connected = yield select(state => state.server.connected);
if (!connected) {
yield put(selectServerRequest(host));
yield take(types.SERVER.SELECT_SUCCESS);
}
yield navigate({ params });
} else {
// search if deep link's server already exists
const serversDB = database.servers;
@ -80,13 +76,18 @@ const handleOpen = function* handleOpen({ params }) {
// do nothing?
}
// if deep link is from a different server
const result = yield RocketChat.getServerInfo(server);
const result = yield RocketChat.getServerInfo(host);
if (!result.success) {
return;
}
Navigation.navigate('OnboardingView', { previousServer: server });
yield delay(1000);
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 { appStart } from '../actions';
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 { toMomentLocale } from '../utils/moment';
import RocketChat from '../lib/rocketchat';
@ -24,7 +26,7 @@ const loginWithPasswordCall = args => RocketChat.loginWithPassword(args);
const loginCall = args => RocketChat.login(args);
const logoutCall = args => RocketChat.logout(args);
const handleLoginRequest = function* handleLoginRequest({ credentials }) {
const handleLoginRequest = function* handleLoginRequest({ credentials, logoutOnError = false }) {
try {
let result;
if (credentials.resume) {
@ -34,7 +36,11 @@ const handleLoginRequest = function* handleLoginRequest({ credentials }) {
}
return yield put(loginSuccess(result));
} 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) {
yield RocketChat.connect({ server, user });
yield RocketChat.connect({ server, user, logoutOnError: true });
yield put(setUser(user));
yield put(actions.appStart('inside'));
} else {