Chore: Migrate logout to Typescript (#3688)

This commit is contained in:
Diego Mello 2022-02-09 17:37:08 -03:00 committed by GitHub
parent 7a5a085067
commit 7d23385555
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 24 deletions

View File

@ -0,0 +1,4 @@
export interface ICertificate {
path: string;
password: string;
}

View File

@ -4,4 +4,6 @@ type TRocketChat = typeof rocketchat;
export interface IRocketChat extends TRocketChat { export interface IRocketChat extends TRocketChat {
sdk: any; sdk: any;
activeUsersSubTimeout: any;
roomsSub: any;
} }

View File

@ -21,6 +21,8 @@ export * from './IUser';
export * from './IServer'; export * from './IServer';
export * from './ILoggedUser'; export * from './ILoggedUser';
export * from './IServerHistory'; export * from './IServerHistory';
export * from './IRocketChat';
export * from './ICertificate';
export interface IBaseScreen<T extends Record<string, object | undefined>, S extends string> { export interface IBaseScreen<T extends Record<string, object | undefined>, S extends string> {
navigation: StackNavigationProp<T, S>; navigation: StackNavigationProp<T, S>;

View File

@ -1,5 +1,6 @@
import * as FileSystem from 'expo-file-system'; import * as FileSystem from 'expo-file-system';
import { Rocketchat as RocketchatClient } from '@rocket.chat/sdk'; import { Rocketchat as RocketchatClient } from '@rocket.chat/sdk';
import Model from '@nozbe/watermelondb/Model';
import { getDeviceToken } from '../../notifications/push'; import { getDeviceToken } from '../../notifications/push';
import { extractHostname } from '../../utils/server'; import { extractHostname } from '../../utils/server';
@ -7,34 +8,38 @@ import { BASIC_AUTH_KEY } from '../../utils/fetch';
import database, { getDatabase } from '../database'; import database, { getDatabase } from '../database';
import RocketChat from '../rocketchat'; import RocketChat from '../rocketchat';
import { useSsl } from '../../utils/url'; import { useSsl } from '../../utils/url';
import log from '../../utils/log';
import { E2E_PRIVATE_KEY, E2E_PUBLIC_KEY, E2E_RANDOM_PASSWORD_KEY } from '../encryption/constants'; import { E2E_PRIVATE_KEY, E2E_PUBLIC_KEY, E2E_RANDOM_PASSWORD_KEY } from '../encryption/constants';
import UserPreferences from '../userPreferences'; import UserPreferences from '../userPreferences';
import { ICertificate, IRocketChat } from '../../definitions';
async function removeServerKeys({ server, userId }) { async function removeServerKeys({ server, userId }: { server: string; userId: string | null }) {
await UserPreferences.removeItem(`${RocketChat.TOKEN_KEY}-${server}`); await UserPreferences.removeItem(`${RocketChat.TOKEN_KEY}-${server}`);
await UserPreferences.removeItem(`${RocketChat.TOKEN_KEY}-${userId}`); if (userId) {
await UserPreferences.removeItem(`${RocketChat.TOKEN_KEY}-${userId}`);
}
await UserPreferences.removeItem(`${BASIC_AUTH_KEY}-${server}`); await UserPreferences.removeItem(`${BASIC_AUTH_KEY}-${server}`);
await UserPreferences.removeItem(`${server}-${E2E_PUBLIC_KEY}`); await UserPreferences.removeItem(`${server}-${E2E_PUBLIC_KEY}`);
await UserPreferences.removeItem(`${server}-${E2E_PRIVATE_KEY}`); await UserPreferences.removeItem(`${server}-${E2E_PRIVATE_KEY}`);
await UserPreferences.removeItem(`${server}-${E2E_RANDOM_PASSWORD_KEY}`); await UserPreferences.removeItem(`${server}-${E2E_RANDOM_PASSWORD_KEY}`);
} }
async function removeSharedCredentials({ server }) { async function removeSharedCredentials({ server }: { server: string }) {
// clear certificate for server - SSL Pinning // clear certificate for server - SSL Pinning
try { try {
const certificate = await UserPreferences.getMapAsync(extractHostname(server)); const certificate = (await UserPreferences.getMapAsync(extractHostname(server))) as ICertificate | null;
if (certificate && certificate.path) { if (certificate?.path) {
await UserPreferences.removeItem(extractHostname(server)); await UserPreferences.removeItem(extractHostname(server));
await FileSystem.deleteAsync(certificate.path); await FileSystem.deleteAsync(certificate.path);
} }
} catch (e) { } catch (e) {
console.log('removeSharedCredentials', e); log(e);
} }
} }
async function removeServerData({ server }) { async function removeServerData({ server }: { server: string }) {
try { try {
const batch = []; const batch: Model[] = [];
const serversDB = database.servers; const serversDB = database.servers;
const userId = await UserPreferences.getStringAsync(`${RocketChat.TOKEN_KEY}-${server}`); const userId = await UserPreferences.getStringAsync(`${RocketChat.TOKEN_KEY}-${server}`);
@ -47,11 +52,11 @@ async function removeServerData({ server }) {
const serverRecord = await serverCollection.find(server); const serverRecord = await serverCollection.find(server);
batch.push(serverRecord.prepareDestroyPermanently()); batch.push(serverRecord.prepareDestroyPermanently());
await serversDB.action(() => serversDB.batch(...batch)); await serversDB.write(() => serversDB.batch(...batch));
await removeSharedCredentials({ server }); await removeSharedCredentials({ server });
await removeServerKeys({ server }); await removeServerKeys({ server, userId });
} catch (e) { } catch (e) {
console.log('removeServerData', e); log(e);
} }
} }
@ -59,16 +64,16 @@ async function removeCurrentServer() {
await UserPreferences.removeItem(RocketChat.CURRENT_SERVER); await UserPreferences.removeItem(RocketChat.CURRENT_SERVER);
} }
async function removeServerDatabase({ server }) { async function removeServerDatabase({ server }: { server: string }) {
try { try {
const db = getDatabase(server); const db = getDatabase(server);
await db.action(() => db.unsafeResetDatabase()); await db.write(() => db.unsafeResetDatabase());
} catch (e) { } catch (e) {
console.log(e); log(e);
} }
} }
export async function removeServer({ server }) { export async function removeServer({ server }: { server: string }): Promise<void> {
try { try {
const userId = await UserPreferences.getStringAsync(`${RocketChat.TOKEN_KEY}-${server}`); const userId = await UserPreferences.getStringAsync(`${RocketChat.TOKEN_KEY}-${server}`);
if (userId) { if (userId) {
@ -88,11 +93,11 @@ export async function removeServer({ server }) {
await removeServerData({ server }); await removeServerData({ server });
await removeServerDatabase({ server }); await removeServerDatabase({ server });
} catch (e) { } catch (e) {
console.log('removeServer', e); log(e);
} }
} }
export default async function logout({ server }) { export default async function logout(this: IRocketChat, { server }: { server: string }): Promise<void> {
if (this.roomsSub) { if (this.roomsSub) {
this.roomsSub.stop(); this.roomsSub.stop();
this.roomsSub = null; this.roomsSub = null;
@ -106,14 +111,14 @@ export default async function logout({ server }) {
try { try {
await this.removePushToken(); await this.removePushToken();
} catch (e) { } catch (e) {
console.log('removePushToken', e); log(e);
} }
try { try {
// RC 0.60.0 // RC 0.60.0
await this.sdk.logout(); await this.sdk.logout();
} catch (e) { } catch (e) {
console.log('logout', e); log(e);
} }
if (this.sdk) { if (this.sdk) {

View File

@ -5,6 +5,7 @@ import * as FileSystem from 'expo-file-system';
import UserPreferences from '../lib/userPreferences'; import UserPreferences from '../lib/userPreferences';
import I18n from '../i18n'; import I18n from '../i18n';
import { extractHostname } from './server'; import { extractHostname } from './server';
import { ICertificate } from '../definitions';
const { SSLPinning } = NativeModules; const { SSLPinning } = NativeModules;
const { documentDirectory } = FileSystem; const { documentDirectory } = FileSystem;
@ -13,11 +14,6 @@ const extractFileScheme = (path: string) => path.replace('file://', ''); // file
const getPath = (name: string) => `${documentDirectory}/${name}`; const getPath = (name: string) => `${documentDirectory}/${name}`;
interface ICertificate {
path: string;
password: string;
}
const persistCertificate = async (name: string, password: string) => { const persistCertificate = async (name: string, password: string) => {
const certificatePath = getPath(name); const certificatePath = getPath(name);
const certificate: ICertificate = { const certificate: ICertificate = {