Files
start-os/ui/src/app/services/auth.service.ts
Matt Hill 08e845228f dont watch patch.data directly in TS (#371)
* dont watch patch.data directly in TS

* installs and uninstalls working

* minor repairs
2021-08-09 13:53:14 -06:00

43 lines
1.2 KiB
TypeScript

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<AuthState> = new BehaviorSubject(AuthState.INITIALIZING)
constructor (
private readonly embassyApi: ApiService,
private readonly storage: Storage,
) { }
async init (): Promise<void> {
const loggedIn = await this.storage.get(this.LOGGED_IN_KEY)
this.authState$.next( loggedIn ? AuthState.VERIFIED : AuthState.UNVERIFIED)
}
watch$ (): Observable<AuthState> {
return this.authState$.pipe(distinctUntilChanged())
}
async login (password: string): Promise<void> {
await this.embassyApi.login({ password })
await this.storage.set(this.LOGGED_IN_KEY, true)
this.authState$.next(AuthState.VERIFIED)
}
async setUnverified (): Promise<void> {
this.authState$.next(AuthState.UNVERIFIED)
}
}