2020-06-04 19:24:11 +02:00
|
|
|
/* eslint-disable import/prefer-default-export */
|
|
|
|
|
2020-06-03 18:07:50 +02:00
|
|
|
import KeychainService from './keychain/KeychainService';
|
2020-11-05 18:58:23 +02:00
|
|
|
const Setting = require('../models/Setting').default;
|
|
|
|
const uuid = require('../uuid').default;
|
2020-06-03 18:07:50 +02:00
|
|
|
|
|
|
|
// This function takes care of initialising both the keychain service and settings.
|
|
|
|
//
|
|
|
|
// Loading the settings became more complicated with the keychain integration. This is because
|
|
|
|
// the settings needs a keychain service, and the keychain service needs a clientId, which
|
|
|
|
// is set dynamically and saved to the settings.
|
|
|
|
// In other words, it's not possible to load the settings without the KS service and it's not
|
|
|
|
// possible to initialise the KS service without the settings.
|
|
|
|
// The solution is to fetch just the client ID directly from the database.
|
2020-11-12 21:13:28 +02:00
|
|
|
export async function loadKeychainServiceAndSettings(KeychainServiceDriver: any) {
|
2020-06-03 18:07:50 +02:00
|
|
|
const clientIdSetting = await Setting.loadOne('clientId');
|
|
|
|
const clientId = clientIdSetting ? clientIdSetting.value : uuid.create();
|
|
|
|
KeychainService.instance().initialize(new KeychainServiceDriver(Setting.value('appId'), clientId));
|
|
|
|
Setting.setKeychainService(KeychainService.instance());
|
|
|
|
await Setting.load();
|
|
|
|
if (!clientIdSetting) Setting.setValue('clientId', clientId);
|
|
|
|
await KeychainService.instance().detectIfKeychainSupported();
|
|
|
|
}
|