1
0
mirror of https://github.com/bitwarden/browser.git synced 2025-01-24 21:41:33 +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() { async ngOnInit() {
// eslint-disable-next-line rxjs-angular/prefer-takeuntil, rxjs/no-async-subscribe // eslint-disable-next-line rxjs-angular/prefer-takeuntil, rxjs/no-async-subscribe
this.route.queryParams.pipe(first()).subscribe(async (qParams) => { 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) { if (qParams.org != null) {
const route = this.router.createUrlTree(["create-organization"], { const route = this.router.createUrlTree(["create-organization"], {
queryParams: { plan: qParams.org }, queryParams: { plan: qParams.org },
@ -115,7 +116,11 @@ export class LoginComponent extends BaseLoginComponent implements OnInit {
this.routerService.setPreviousUrl(route.toString()); 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) { if (qParams.sponsorshipToken != null) {
const route = this.router.createUrlTree(["setup/families-for-enterprise"], { const route = this.router.createUrlTree(["setup/families-for-enterprise"], {
queryParams: { token: qParams.sponsorshipToken }, queryParams: { token: qParams.sponsorshipToken },

View File

@ -103,12 +103,14 @@ export class LoginComponent extends CaptchaProtectedComponent implements OnInit,
const queryParamsEmail = params.email; 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) { if (queryParamsEmail != null && queryParamsEmail.indexOf("@") > -1) {
this.formGroup.controls.email.setValue(queryParamsEmail); this.formGroup.controls.email.setValue(queryParamsEmail);
this.paramEmailSet = true; this.paramEmailSet = true;
} }
}); });
// if there is no email in the query params, attempt to load email settings from loginEmailService
if (!this.paramEmailSet) { if (!this.paramEmailSet) {
await this.loadEmailSettings(); await this.loadEmailSettings();
} }

View File

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

View File

@ -2,9 +2,10 @@ import { CommonModule } from "@angular/common";
import { Component, OnInit } from "@angular/core"; import { Component, OnInit } from "@angular/core";
import { FormBuilder, ReactiveFormsModule, Validators } from "@angular/forms"; import { FormBuilder, ReactiveFormsModule, Validators } from "@angular/forms";
import { ActivatedRoute, RouterModule } from "@angular/router"; 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 { JslibModule } from "@bitwarden/angular/jslib.module";
import { LoginEmailServiceAbstraction } from "@bitwarden/auth/common";
import { import {
AsyncActionsModule, AsyncActionsModule,
ButtonModule, ButtonModule,
@ -27,8 +28,6 @@ import {
], ],
}) })
export class LoginComponentV2 implements OnInit { export class LoginComponentV2 implements OnInit {
protected paramEmailSet = false;
protected formGroup = this.formBuilder.group({ protected formGroup = this.formBuilder.group({
email: ["", [Validators.required, Validators.email]], email: ["", [Validators.required, Validators.email]],
rememberEmail: [false], rememberEmail: [false],
@ -39,9 +38,12 @@ export class LoginComponentV2 implements OnInit {
constructor( constructor(
private activatedRoute: ActivatedRoute, private activatedRoute: ActivatedRoute,
private formBuilder: FormBuilder, private formBuilder: FormBuilder,
private loginEmailService: LoginEmailServiceAbstraction,
) {} ) {}
async ngOnInit(): Promise<void> { async ngOnInit(): Promise<void> {
let paramEmailIsSet = false;
this.activatedRoute.queryParams.pipe(takeUntil(this.destroy$)).subscribe((params) => { this.activatedRoute.queryParams.pipe(takeUntil(this.destroy$)).subscribe((params) => {
if (!params) { if (!params) {
return; return;
@ -51,12 +53,11 @@ export class LoginComponentV2 implements OnInit {
if (qParamsEmail?.indexOf("@") > -1) { if (qParamsEmail?.indexOf("@") > -1) {
this.formGroup.controls.email.setValue(qParamsEmail); this.formGroup.controls.email.setValue(qParamsEmail);
paramEmailIsSet = true;
this.paramEmailSet = true;
} }
}); });
if (!this.paramEmailSet) { if (!paramEmailIsSet) {
await this.loadEmailSettings(); await this.loadEmailSettings();
} }
} }
@ -65,5 +66,22 @@ export class LoginComponentV2 implements OnInit {
async validateEmail() {} 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
}
}
}
} }