[FIX] Don't run UserDefaults credentials on Android (#1015)

* 🐛 Fix native credentials (android)

* Fix migration loop
This commit is contained in:
Djorkaeff Alexandre 2019-06-27 09:35:20 -03:00 committed by Diego Mello
parent 255ea84599
commit 636518b6b3
1 changed files with 32 additions and 23 deletions

View File

@ -1,3 +1,4 @@
import { AsyncStorage } from 'react-native';
import { put, takeLatest, all } from 'redux-saga/effects'; import { put, takeLatest, all } from 'redux-saga/effects';
import SplashScreen from 'react-native-splash-screen'; import SplashScreen from 'react-native-splash-screen';
import RNUserDefaults from 'rn-user-defaults'; import RNUserDefaults from 'rn-user-defaults';
@ -14,10 +15,15 @@ import database from '../lib/realm';
import { import {
SERVERS, SERVER_ICON, SERVER_NAME, SERVER_URL, TOKEN, USER_ID SERVERS, SERVER_ICON, SERVER_NAME, SERVER_URL, TOKEN, USER_ID
} from '../constants/userDefaults'; } from '../constants/userDefaults';
import { isIOS } from '../utils/deviceInfo';
const restore = function* restore() { const restore = function* restore() {
try { try {
yield RNUserDefaults.setName('group.ios.chat.rocket'); let hasMigration;
if (isIOS) {
yield RNUserDefaults.setName('group.ios.chat.rocket');
hasMigration = yield AsyncStorage.getItem('hasMigration');
}
let { token, server } = yield all({ let { token, server } = yield all({
token: RNUserDefaults.get(RocketChat.TOKEN_KEY), token: RNUserDefaults.get(RocketChat.TOKEN_KEY),
@ -25,30 +31,33 @@ const restore = function* restore() {
}); });
// get native credentials // get native credentials
const { serversDB } = database.databases; if (isIOS && !hasMigration) {
const servers = yield RNUserDefaults.objectForKey(SERVERS); const { serversDB } = database.databases;
if (servers) { const servers = yield RNUserDefaults.objectForKey(SERVERS);
serversDB.write(() => { if (servers) {
servers.forEach(async(serverItem) => { serversDB.write(() => {
const serverInfo = { servers.forEach(async(serverItem) => {
id: serverItem[SERVER_URL], const serverInfo = {
name: serverItem[SERVER_NAME], id: serverItem[SERVER_URL],
iconURL: serverItem[SERVER_ICON] name: serverItem[SERVER_NAME],
}; iconURL: serverItem[SERVER_ICON]
try { };
serversDB.create('servers', serverInfo, true); try {
await RNUserDefaults.set(`${ RocketChat.TOKEN_KEY }-${ serverInfo.id }`, serverItem[USER_ID]); serversDB.create('servers', serverInfo, true);
} catch (e) { await RNUserDefaults.set(`${ RocketChat.TOKEN_KEY }-${ serverInfo.id }`, serverItem[USER_ID]);
log('err_create_servers', e); } catch (e) {
} log('err_create_servers', e);
}
});
}); });
}); yield AsyncStorage.setItem('hasMigration', true);
} }
// if not have current // if not have current
if (servers.length !== 0 && (!token || !server)) { if (servers && servers.length !== 0 && (!token || !server)) {
server = servers[0][SERVER_URL]; server = servers[0][SERVER_URL];
token = servers[0][TOKEN]; token = servers[0][TOKEN];
}
} }
const sortPreferences = yield RocketChat.getSortPreferences(); const sortPreferences = yield RocketChat.getSortPreferences();