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 {
sdk: any;
activeUsersSubTimeout: any;
roomsSub: any;
}

View File

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

View File

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

View File

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