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

handle loading email settings

This commit is contained in:
rr-bw 2024-08-21 14:54:07 -07:00
parent af91d39c57
commit 2b90cbb3e5
No known key found for this signature in database
GPG Key ID: 3FA13C3ADEE51D5D
4 changed files with 33 additions and 9 deletions

View File

@ -108,6 +108,7 @@ export class LoginComponent extends BaseLoginComponent implements OnInit {
async ngOnInit() {
// eslint-disable-next-line rxjs-angular/prefer-takeuntil, rxjs/no-async-subscribe
this.route.queryParams.pipe(first()).subscribe(async (qParams) => {
// If there is an query parameter called 'org', set previousUrl to `/create-organization?org=paramValue`
if (qParams.org != null) {
const route = this.router.createUrlTree(["create-organization"], {
queryParams: { plan: qParams.org },
@ -115,7 +116,11 @@ export class LoginComponent extends BaseLoginComponent implements OnInit {
this.routerService.setPreviousUrl(route.toString());
}
// Are they coming from an email for sponsoring a families organization
/**
* If there is a query parameter called 'sponsorshipToken', that means they are coming
* from an email for sponsoring a families organization. If so, then set the prevousUrl
* to `/setup/families-for-enterprise?token=paramValue`
*/
if (qParams.sponsorshipToken != null) {
const route = this.router.createUrlTree(["setup/families-for-enterprise"], {
queryParams: { token: qParams.sponsorshipToken },

View File

@ -103,12 +103,14 @@ export class LoginComponent extends CaptchaProtectedComponent implements OnInit,
const queryParamsEmail = params.email;
// If there is an email in the query params, set that email as the form field value
if (queryParamsEmail != null && queryParamsEmail.indexOf("@") > -1) {
this.formGroup.controls.email.setValue(queryParamsEmail);
this.paramEmailSet = true;
}
});
// if there is no email in the query params, attempt to load email settings from loginEmailService
if (!this.paramEmailSet) {
await this.loadEmailSettings();
}

View File

@ -1,5 +1,4 @@
<form [bitSubmit]="submit" [formGroup]="formGroup">
<p>New Component</p>
<!-------------------------
UI STATE 1: Email Entry
-------------------------->

View File

@ -2,9 +2,10 @@ import { CommonModule } from "@angular/common";
import { Component, OnInit } from "@angular/core";
import { FormBuilder, ReactiveFormsModule, Validators } from "@angular/forms";
import { ActivatedRoute, RouterModule } from "@angular/router";
import { Subject, takeUntil } from "rxjs";
import { firstValueFrom, Subject, takeUntil } from "rxjs";
import { JslibModule } from "@bitwarden/angular/jslib.module";
import { LoginEmailServiceAbstraction } from "@bitwarden/auth/common";
import {
AsyncActionsModule,
ButtonModule,
@ -27,8 +28,6 @@ import {
],
})
export class LoginComponentV2 implements OnInit {
protected paramEmailSet = false;
protected formGroup = this.formBuilder.group({
email: ["", [Validators.required, Validators.email]],
rememberEmail: [false],
@ -39,9 +38,12 @@ export class LoginComponentV2 implements OnInit {
constructor(
private activatedRoute: ActivatedRoute,
private formBuilder: FormBuilder,
private loginEmailService: LoginEmailServiceAbstraction,
) {}
async ngOnInit(): Promise<void> {
let paramEmailIsSet = false;
this.activatedRoute.queryParams.pipe(takeUntil(this.destroy$)).subscribe((params) => {
if (!params) {
return;
@ -51,12 +53,11 @@ export class LoginComponentV2 implements OnInit {
if (qParamsEmail?.indexOf("@") > -1) {
this.formGroup.controls.email.setValue(qParamsEmail);
this.paramEmailSet = true;
paramEmailIsSet = true;
}
});
if (!this.paramEmailSet) {
if (!paramEmailIsSet) {
await this.loadEmailSettings();
}
}
@ -65,5 +66,22 @@ export class LoginComponentV2 implements OnInit {
async validateEmail() {}
private async loadEmailSettings() {}
private async loadEmailSettings() {
// Try to load the email from memory first
const email = this.loginEmailService.getEmail();
const rememberEmail = this.loginEmailService.getRememberEmail();
if (email) {
this.formGroup.controls.email.setValue(email);
this.formGroup.controls.rememberEmail.setValue(rememberEmail);
} else {
// If there is no email in memory, check for a storedEmail on disk
const storedEmail = await firstValueFrom(this.loginEmailService.storedEmail$);
if (storedEmail) {
this.formGroup.controls.email.setValue(storedEmail);
this.formGroup.controls.rememberEmail.setValue(true); // If there is a storedEmail, rememberEmail defaults to true
}
}
}
}