1
0
mirror of https://github.com/bitwarden/browser.git synced 2025-02-08 00:01:28 +01:00

Establish account service observables and update methods

This commit is contained in:
Matt Gibson 2023-09-27 17:33:01 -04:00 committed by Justin Baur
parent 0a9c8a416a
commit 4f97c7e283
No known key found for this signature in database
GPG Key ID: 46438BBD28B69008

View File

@ -1,4 +1,11 @@
import { BehaviorSubject, Subject } from "rxjs"; import {
BehaviorSubject,
Subject,
combineLatestWith,
map,
distinctUntilChanged,
share,
} from "rxjs";
import { InternalAccountService } from "../../auth/abstractions/account.service"; import { InternalAccountService } from "../../auth/abstractions/account.service";
import { LogService } from "../../platform/abstractions/log.service"; import { LogService } from "../../platform/abstractions/log.service";
@ -8,20 +15,27 @@ import { AuthenticationStatus } from "../enums/authentication-status";
export class AccountServiceImplementation implements InternalAccountService { export class AccountServiceImplementation implements InternalAccountService {
private accounts = new BehaviorSubject<Record<UserId, AuthenticationStatus>>({}); private accounts = new BehaviorSubject<Record<UserId, AuthenticationStatus>>({});
private activeAccount = new BehaviorSubject<{ private activeAccountId = new BehaviorSubject<UserId | undefined>(undefined);
id: UserId | undefined;
status: AuthenticationStatus | undefined;
}>({ id: undefined, status: undefined });
private lock = new Subject<UserId>(); private lock = new Subject<UserId>();
private logout = new Subject<UserId>(); private logout = new Subject<UserId>();
accounts$ = this.accounts.asObservable(); accounts$ = this.accounts.asObservable();
activeAccount$ = this.activeAccount.asObservable(); activeAccount$ = this.activeAccountId.pipe(
combineLatestWith(this.accounts$),
map(([id, accounts]) => (id ? { id, status: accounts[id] } : undefined)),
distinctUntilChanged((a, b) => a.id === b.id && a.status === b.status),
share()
);
accountLock$ = this.lock.asObservable(); accountLock$ = this.lock.asObservable();
accountLogout$ = this.logout.asObservable(); accountLogout$ = this.logout.asObservable();
constructor(private messagingService: MessagingService, private logService: LogService) {} constructor(private messagingService: MessagingService, private logService: LogService) {}
setAccountStatus(userId: UserId, status: AuthenticationStatus): void { setAccountStatus(userId: UserId, status: AuthenticationStatus): void {
if (this.accounts.value[userId] === status) {
// Do not emit on no change
return;
}
this.accounts.value[userId] = status; this.accounts.value[userId] = status;
this.accounts.next(this.accounts.value); this.accounts.next(this.accounts.value);
if (status === AuthenticationStatus.LoggedOut) { if (status === AuthenticationStatus.LoggedOut) {
@ -32,9 +46,15 @@ export class AccountServiceImplementation implements InternalAccountService {
} }
switchAccount(userId: UserId) { switchAccount(userId: UserId) {
if (this.accounts.value[userId] != null) { if (userId == null) {
// indicates no account is active
this.activeAccountId.next(undefined);
}
if (this.accounts.value[userId] == null) {
throw new Error("Account does not exist"); throw new Error("Account does not exist");
} }
this.activeAccountId.next(userId);
} }
// TODO: update to use our own account status settings. // TODO: update to use our own account status settings.