signal-desktop/ts/util/connectToServerWithStoredCredentials.ts
2021-06-14 17:09:37 -07:00

26 lines
804 B
TypeScript

// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { WebAPIConnectType, WebAPIType } from '../textsecure/WebAPI';
import { StorageInterface } from '../types/Storage.d';
export function connectToServerWithStoredCredentials(
WebAPI: WebAPIConnectType,
storage: Pick<StorageInterface, 'get'>
): WebAPIType {
const username = storage.get('uuid_id') || storage.get('number_id');
if (typeof username !== 'string') {
throw new Error(
'Username in storage was not a string. Cannot connect to WebAPI'
);
}
const password = storage.get('password');
if (typeof password !== 'string') {
throw new Error(
'Password in storage was not a string. Cannot connect to WebAPI'
);
}
return WebAPI.connect({ username, password });
}