startup.service.ts raw

   1  import { inject, Injectable } from '@angular/core';
   2  import { Router } from '@angular/router';
   3  import { LoggerService } from '../logger/logger.service';
   4  import {
   5    StorageService,
   6    StorageServiceConfig,
   7  } from '../storage/storage.service';
   8  import { SyncFlow } from '../storage/types';
   9  
  10  @Injectable({
  11    providedIn: 'root',
  12  })
  13  export class StartupService {
  14    readonly #logger = inject(LoggerService);
  15    readonly #storage = inject(StorageService);
  16    readonly #router = inject(Router);
  17  
  18    async startOver(storageConfig: StorageServiceConfig) {
  19      this.#storage.initialize(storageConfig);
  20  
  21      // Step 0:
  22      storageConfig.browserSyncNoHandler.setIgnoreProperties(
  23        storageConfig.signerMetaHandler.metaProperties
  24      );
  25  
  26      // Step 1: Load the user settings
  27      const signerMetaData = await this.#storage.loadSignerMetaData();
  28      if (typeof signerMetaData?.syncFlow === 'undefined') {
  29        // First run — force local-only storage (no sync to Mozilla servers).
  30        await this.#storage.enableBrowserSyncFlow(SyncFlow.NO_SYNC);
  31        this.#router.navigateByUrl('/vault-create/home');
  32        return;
  33      }
  34      this.#storage.enableBrowserSyncFlow(signerMetaData.syncFlow);
  35  
  36      // Load the browser session data.
  37      const browserSessionData = await this.#storage.loadBrowserSessionData();
  38  
  39      if (!browserSessionData) {
  40        await this.#initializeFlow_A();
  41      } else {
  42        await this.#initializeFlow_B();
  43      }
  44    }
  45  
  46    async #initializeFlow_A() {
  47      // Starting with NO browser session data available.
  48      //
  49      // This could be because the browser sync data was
  50      // never loaded before OR it was attempted, but
  51      // there is no browser sync data.
  52  
  53      this.#logger.log('No browser session data available.');
  54  
  55      // Check if there is NO browser sync data.
  56      const browserSyncData = await this.#storage.loadAndMigrateBrowserSyncData();
  57      if (browserSyncData) {
  58        // There is browser sync data. Route to the VAULT LOGIN to enable the session.
  59        this.#router.navigateByUrl('/vault-login');
  60      } else {
  61        // There is NO browser sync data. Route to the VAULT CREATION to enable the session.
  62        this.#router.navigateByUrl('/vault-create/home');
  63      }
  64    }
  65  
  66    async #initializeFlow_B() {
  67      // Stating with browser session data available. The user has already unlocked the vault before.
  68      // Route to VAULT HOME.
  69  
  70      this.#logger.log('Browser session data is available.');
  71  
  72      // Also load the browser sync data. This is needed, if the user adds or deletes anything.
  73      await this.#storage.loadAndMigrateBrowserSyncData();
  74  
  75      const selectedIdentityId =
  76        this.#storage.getBrowserSessionHandler().browserSessionData
  77          ?.selectedIdentityId;
  78  
  79      this.#router.navigateByUrl(
  80        `/home/${selectedIdentityId ? 'identity' : 'identities'}`
  81      );
  82    }
  83  }
  84