[PM-2910] Add Account Switching Page (#6701)
* Collate browser header concerns into component
Header component has a slots field with a left, center, right, and a
right-most location for a current account, which will link to an account
switcher.
* Use feature flag if OK for production eventually
* Make sure centered content centered
* Allow for disabling header theming for login page
visual gitches exist for links and buttons, due to specifications futher down in the header, but those
items shouldn't use the `no-theme` option. For now, it's just for the
login screen
* Add Account Switching Component
* Collate browser header concerns into component
Header component has a slots field with a left, center, right, and a
right-most location for a current account, which will link to an account
switcher.
* Use feature flag if OK for production eventually
* Add Account Switching Component
* Fix Rebase Issues
* Remove Comments
* Move AccountSwitcher Logic Into Service
* Rename File
* Move Router to Component
* Add Tests for AccountSwitcherService
---------
Co-authored-by: Matt Gibson <mgibson@bitwarden.com>
2023-10-30 16:34:42 +01:00
|
|
|
import { Injectable } from "@angular/core";
|
|
|
|
import { combineLatest, map } from "rxjs";
|
|
|
|
|
|
|
|
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
|
|
|
|
import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service";
|
|
|
|
import { StateService } from "@bitwarden/common/platform/abstractions/state.service";
|
|
|
|
import { UserId } from "@bitwarden/common/types/guid";
|
|
|
|
|
|
|
|
const SPECIAL_ADD_ACCOUNT_VALUE = "addAccount";
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: "root",
|
|
|
|
})
|
|
|
|
export class AccountSwitcherService {
|
|
|
|
constructor(
|
|
|
|
private accountService: AccountService,
|
|
|
|
private stateService: StateService,
|
2023-11-29 22:15:20 +01:00
|
|
|
private messagingService: MessagingService,
|
[PM-2910] Add Account Switching Page (#6701)
* Collate browser header concerns into component
Header component has a slots field with a left, center, right, and a
right-most location for a current account, which will link to an account
switcher.
* Use feature flag if OK for production eventually
* Make sure centered content centered
* Allow for disabling header theming for login page
visual gitches exist for links and buttons, due to specifications futher down in the header, but those
items shouldn't use the `no-theme` option. For now, it's just for the
login screen
* Add Account Switching Component
* Collate browser header concerns into component
Header component has a slots field with a left, center, right, and a
right-most location for a current account, which will link to an account
switcher.
* Use feature flag if OK for production eventually
* Add Account Switching Component
* Fix Rebase Issues
* Remove Comments
* Move AccountSwitcher Logic Into Service
* Rename File
* Move Router to Component
* Add Tests for AccountSwitcherService
---------
Co-authored-by: Matt Gibson <mgibson@bitwarden.com>
2023-10-30 16:34:42 +01:00
|
|
|
) {}
|
|
|
|
|
|
|
|
get accountOptions$() {
|
|
|
|
return combineLatest([this.accountService.accounts$, this.accountService.activeAccount$]).pipe(
|
|
|
|
map(([accounts, activeAccount]) => {
|
|
|
|
const accountEntries = Object.entries(accounts);
|
|
|
|
// Accounts shouldn't ever be more than 5 but just in case do a greater than
|
|
|
|
const hasMaxAccounts = accountEntries.length >= 5;
|
|
|
|
const options: { name: string; id: string; isSelected: boolean }[] = accountEntries.map(
|
|
|
|
([id, account]) => {
|
|
|
|
return {
|
|
|
|
name: account.name ?? account.email,
|
|
|
|
id: id,
|
|
|
|
isSelected: id === activeAccount?.id,
|
|
|
|
};
|
2023-11-29 22:15:20 +01:00
|
|
|
},
|
[PM-2910] Add Account Switching Page (#6701)
* Collate browser header concerns into component
Header component has a slots field with a left, center, right, and a
right-most location for a current account, which will link to an account
switcher.
* Use feature flag if OK for production eventually
* Make sure centered content centered
* Allow for disabling header theming for login page
visual gitches exist for links and buttons, due to specifications futher down in the header, but those
items shouldn't use the `no-theme` option. For now, it's just for the
login screen
* Add Account Switching Component
* Collate browser header concerns into component
Header component has a slots field with a left, center, right, and a
right-most location for a current account, which will link to an account
switcher.
* Use feature flag if OK for production eventually
* Add Account Switching Component
* Fix Rebase Issues
* Remove Comments
* Move AccountSwitcher Logic Into Service
* Rename File
* Move Router to Component
* Add Tests for AccountSwitcherService
---------
Co-authored-by: Matt Gibson <mgibson@bitwarden.com>
2023-10-30 16:34:42 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!hasMaxAccounts) {
|
|
|
|
options.push({
|
|
|
|
name: "Add Account",
|
|
|
|
id: SPECIAL_ADD_ACCOUNT_VALUE,
|
|
|
|
isSelected: activeAccount?.id == null,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
2023-11-29 22:15:20 +01:00
|
|
|
}),
|
[PM-2910] Add Account Switching Page (#6701)
* Collate browser header concerns into component
Header component has a slots field with a left, center, right, and a
right-most location for a current account, which will link to an account
switcher.
* Use feature flag if OK for production eventually
* Make sure centered content centered
* Allow for disabling header theming for login page
visual gitches exist for links and buttons, due to specifications futher down in the header, but those
items shouldn't use the `no-theme` option. For now, it's just for the
login screen
* Add Account Switching Component
* Collate browser header concerns into component
Header component has a slots field with a left, center, right, and a
right-most location for a current account, which will link to an account
switcher.
* Use feature flag if OK for production eventually
* Add Account Switching Component
* Fix Rebase Issues
* Remove Comments
* Move AccountSwitcher Logic Into Service
* Rename File
* Move Router to Component
* Add Tests for AccountSwitcherService
---------
Co-authored-by: Matt Gibson <mgibson@bitwarden.com>
2023-10-30 16:34:42 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async selectAccount(id: string) {
|
|
|
|
if (id === SPECIAL_ADD_ACCOUNT_VALUE) {
|
|
|
|
await this.stateService.setActiveUser(null);
|
|
|
|
await this.stateService.setRememberedEmail(null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-29 15:59:50 +01:00
|
|
|
await this.accountService.switchAccount(id as UserId);
|
[PM-2910] Add Account Switching Page (#6701)
* Collate browser header concerns into component
Header component has a slots field with a left, center, right, and a
right-most location for a current account, which will link to an account
switcher.
* Use feature flag if OK for production eventually
* Make sure centered content centered
* Allow for disabling header theming for login page
visual gitches exist for links and buttons, due to specifications futher down in the header, but those
items shouldn't use the `no-theme` option. For now, it's just for the
login screen
* Add Account Switching Component
* Collate browser header concerns into component
Header component has a slots field with a left, center, right, and a
right-most location for a current account, which will link to an account
switcher.
* Use feature flag if OK for production eventually
* Add Account Switching Component
* Fix Rebase Issues
* Remove Comments
* Move AccountSwitcher Logic Into Service
* Rename File
* Move Router to Component
* Add Tests for AccountSwitcherService
---------
Co-authored-by: Matt Gibson <mgibson@bitwarden.com>
2023-10-30 16:34:42 +01:00
|
|
|
this.messagingService.send("switchAccount", { userId: id });
|
|
|
|
}
|
|
|
|
}
|