import { Injectable } from '@angular/core' import { BehaviorSubject, Observable } from 'rxjs' import { distinctUntilChanged } from 'rxjs/operators' import { ApiService } from './api/embassy/embassy-api.service' import { Storage } from '@ionic/storage' export enum AuthState { UNVERIFIED, VERIFIED, INITIALIZING, } @Injectable({ providedIn: 'root', }) export class AuthService { private readonly LOGGED_IN_KEY = 'loggedInKey' private readonly authState$: BehaviorSubject = new BehaviorSubject(AuthState.INITIALIZING) constructor ( private readonly embassyApi: ApiService, private readonly storage: Storage, ) { } async init (): Promise { const loggedIn = await this.storage.get(this.LOGGED_IN_KEY) this.authState$.next( loggedIn ? AuthState.VERIFIED : AuthState.UNVERIFIED) } watch$ (): Observable { return this.authState$.pipe(distinctUntilChanged()) } async login (password: string): Promise { await this.embassyApi.login({ password }) await this.storage.set(this.LOGGED_IN_KEY, true) this.authState$.next(AuthState.VERIFIED) } async setUnverified (): Promise { this.authState$.next(AuthState.UNVERIFIED) } }