[FIX] Select user from native credentials (#1266)

This commit is contained in:
Djorkaeff Alexandre 2019-10-02 09:55:25 -03:00 committed by Diego Mello
parent 67fb14fa9d
commit 6fd722a0d8
2 changed files with 21 additions and 23 deletions

View File

@ -231,11 +231,11 @@ const RocketChat = {
const userCollections = serversDB.collections.get('users'); const userCollections = serversDB.collections.get('users');
let user = null; let user = null;
if (userId) { if (userId) {
user = await userCollections.find(userId); const userRecord = await userCollections.find(userId);
user = { user = {
id: user.id, id: userRecord.id,
token: user.token, token: userRecord.token,
username: user.username username: userRecord.username
}; };
} }
reduxStore.dispatch(shareSetUser({ reduxStore.dispatch(shareSetUser({

View File

@ -61,31 +61,29 @@ const handleSelectServer = function* handleSelectServer({ server, version, fetch
let user = null; let user = null;
if (userId) { if (userId) {
try { try {
user = yield userCollections.find(userId); const userRecord = yield userCollections.find(userId);
user = { user = {
id: user.id, id: userRecord.id,
token: user.token, token: userRecord.token,
username: user.username, username: userRecord.username,
name: user.name, name: userRecord.name,
language: user.language, language: userRecord.language,
status: user.status, status: userRecord.status,
roles: user.roles roles: userRecord.roles
}; };
user = { ...user, roles: JSON.parse(user.roles) };
} catch (e) { } 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); if (user) {
const userCredentials = servers && servers.find(srv => srv[SERVER_URL] === server); yield RocketChat.connect({ server, user });
const userLogin = userCredentials && { yield put(setUser(user));
token: userCredentials[TOKEN]
};
if (user || userLogin) {
yield RocketChat.connect({ server, user: user || userLogin });
yield put(setUser(user || userLogin));
yield put(actions.appStart('inside')); yield put(actions.appStart('inside'));
} else { } else {
yield RocketChat.connect({ server }); yield RocketChat.connect({ server });