From 6fd722a0d80e8c42cb70a011d8f337e3b8832d8c Mon Sep 17 00:00:00 2001 From: Djorkaeff Alexandre Date: Wed, 2 Oct 2019 09:55:25 -0300 Subject: [PATCH] [FIX] Select user from native credentials (#1266) --- app/lib/rocketchat.js | 8 ++++---- app/sagas/selectServer.js | 36 +++++++++++++++++------------------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/app/lib/rocketchat.js b/app/lib/rocketchat.js index db8344ca..e6254845 100644 --- a/app/lib/rocketchat.js +++ b/app/lib/rocketchat.js @@ -231,11 +231,11 @@ const RocketChat = { const userCollections = serversDB.collections.get('users'); let user = null; if (userId) { - user = await userCollections.find(userId); + const userRecord = await userCollections.find(userId); user = { - id: user.id, - token: user.token, - username: user.username + id: userRecord.id, + token: userRecord.token, + username: userRecord.username }; } reduxStore.dispatch(shareSetUser({ diff --git a/app/sagas/selectServer.js b/app/sagas/selectServer.js index d27f71ed..83fdc297 100644 --- a/app/sagas/selectServer.js +++ b/app/sagas/selectServer.js @@ -61,31 +61,29 @@ const handleSelectServer = function* handleSelectServer({ server, version, fetch let user = null; if (userId) { try { - user = yield userCollections.find(userId); + const userRecord = yield userCollections.find(userId); user = { - id: user.id, - token: user.token, - username: user.username, - name: user.name, - language: user.language, - status: user.status, - roles: user.roles + id: userRecord.id, + token: userRecord.token, + username: userRecord.username, + name: userRecord.name, + language: userRecord.language, + status: userRecord.status, + roles: userRecord.roles }; - user = { ...user, roles: JSON.parse(user.roles) }; } catch (e) { - // do nothing? + // We only run it if not has user on DB + const servers = yield RNUserDefaults.objectForKey(SERVERS); + const userCredentials = servers && servers.find(srv => srv[SERVER_URL] === server); + user = userCredentials && { + token: userCredentials[TOKEN] + }; } } - const servers = yield RNUserDefaults.objectForKey(SERVERS); - const userCredentials = servers && servers.find(srv => srv[SERVER_URL] === server); - const userLogin = userCredentials && { - token: userCredentials[TOKEN] - }; - - if (user || userLogin) { - yield RocketChat.connect({ server, user: user || userLogin }); - yield put(setUser(user || userLogin)); + if (user) { + yield RocketChat.connect({ server, user }); + yield put(setUser(user)); yield put(actions.appStart('inside')); } else { yield RocketChat.connect({ server });