Skip to content

Commit

Permalink
refactor ngOnInit to use two paths
Browse files Browse the repository at this point in the history
  • Loading branch information
rr-bw committed Nov 7, 2024
1 parent b21edf1 commit dd610e3
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
block
buttonType="secondary"
class="tw-mt-4"
(click)="startAuthRequestLogin()"
(click)="startStandardAuthRequestLogin()"
>
{{ "resendNotification" | i18n }}
</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,82 +110,108 @@ export class LoginViaAuthRequestComponent implements OnInit, OnDestroy {
}

async ngOnInit(): Promise<void> {
// Check if we are in an admin auth request flow
// Get the authStatus early because we use it in both flows
this.authStatus = await firstValueFrom(this.authService.activeAccountStatus$);

Check warning on line 114 in libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts#L114

Added line #L114 was not covered by tests

/**
* The LoginViaAuthRequestComponent handles both the `login-with-device` and
* the `admin-approval-requested` routes. Therefore we check the route to determine
* which flow to initialize.
*/
if (this.router.isActive("admin-approval-requested", matchOptions)) {
this.state = State.AdminAuthRequest;
this.backToRoute = "/login-initiated";
await this.initAdminAuthRequestFlow();

Check warning on line 122 in libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts#L122

Added line #L122 was not covered by tests
} else {
await this.initStandardAuthRequestFlow();

Check warning on line 124 in libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts#L124

Added line #L124 was not covered by tests
}
}

// Get email based on auth request flow
if (this.state === State.AdminAuthRequest) {
// Get email from state for admin auth requests because it is available and also
// prevents it from being lost on refresh as the loginEmailService email does not persist.
this.email = await firstValueFrom(
this.accountService.activeAccount$.pipe(map((a) => a?.email)),
);
private async initAdminAuthRequestFlow(): Promise<void> {
this.state = State.AdminAuthRequest;
this.backToRoute = "/login-initiated";

Check warning on line 130 in libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts#L129-L130

Added lines #L129 - L130 were not covered by tests

// Get email from state for admin auth requests because it is available and also
// prevents it from being lost on refresh as the loginEmailService email does not persist.
this.email = await firstValueFrom(

Check warning on line 134 in libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts#L134

Added line #L134 was not covered by tests
this.accountService.activeAccount$.pipe(map((a) => a?.email)),
);

if (!this.email) {
await this.handleMissingEmail();
return;

Check warning on line 140 in libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts#L139-L140

Added lines #L139 - L140 were not covered by tests
}

// We only allow a single admin approval request to be active at a time
// so we must check state to see if we have an existing one or not
const userId = (await firstValueFrom(this.accountService.activeAccount$)).id;
const existingAdminAuthRequest = await this.authRequestService.getAdminAuthRequest(userId);

Check warning on line 146 in libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts#L145-L146

Added lines #L145 - L146 were not covered by tests

if (existingAdminAuthRequest) {
await this.handleExistingAdminAuthRequest(existingAdminAuthRequest, userId);

Check warning on line 149 in libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts#L149

Added line #L149 was not covered by tests
} else {
this.email = await firstValueFrom(this.loginEmailService.loginEmail$);
await this.startAdminAuthRequestLogin();

Check warning on line 151 in libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts#L151

Added line #L151 was not covered by tests
}
}

// If email is missing, show error toast and redirect
if (!this.email) {
this.toastService.showToast({
variant: "error",
title: null,
message: this.i18nService.t("userEmailMissing"),
});
private async initStandardAuthRequestFlow(): Promise<void> {
this.state = State.StandardAuthRequest;
this.backToRoute = "/login";

Check warning on line 157 in libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts#L156-L157

Added lines #L156 - L157 were not covered by tests

await this.router.navigate([this.backToRoute]);
this.email = await firstValueFrom(this.loginEmailService.loginEmail$);

Check warning on line 159 in libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts#L159

Added line #L159 was not covered by tests

if (!this.email) {
await this.handleMissingEmail();
return;

Check warning on line 163 in libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts#L162-L163

Added lines #L162 - L163 were not covered by tests
}

this.authStatus = await firstValueFrom(this.authService.activeAccountStatus$);
await this.startStandardAuthRequestLogin();

Check warning on line 166 in libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts#L166

Added line #L166 was not covered by tests
}

if (this.state === State.AdminAuthRequest) {
// We only allow a single admin approval request to be active at a time
// so we must check state to see if we have an existing one or not
const userId = (await firstValueFrom(this.accountService.activeAccount$)).id;
const existingAdminAuthRequest = await this.authRequestService.getAdminAuthRequest(userId);
private async handleMissingEmail(): Promise<void> {
this.toastService.showToast({

Check warning on line 170 in libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts#L170

Added line #L170 was not covered by tests
variant: "error",
title: null,
message: this.i18nService.t("userEmailMissing"),
});

if (existingAdminAuthRequest) {
await this.handleExistingAdminAuthRequest(existingAdminAuthRequest, userId);
} else {
await this.startAuthRequestLogin();
}
} else {
// Standard auth request flow
await this.startAuthRequestLogin();
}
await this.router.navigate([this.backToRoute]);

Check warning on line 176 in libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts#L176

Added line #L176 was not covered by tests
}

async ngOnDestroy(): Promise<void> {
await this.anonymousHubService.stopHubConnection();

Check warning on line 180 in libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts#L180

Added line #L180 was not covered by tests
}

protected async startAuthRequestLogin(): Promise<void> {
this.showResendNotification = false;

private async startAdminAuthRequestLogin(): Promise<void> {
try {
let authRequestResponse: AuthRequestResponse;
await this.buildAuthRequest(AuthRequestType.AdminApproval);

Check warning on line 185 in libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts#L184-L185

Added lines #L184 - L185 were not covered by tests

if (this.state === State.AdminAuthRequest) {
await this.buildAuthRequest(AuthRequestType.AdminApproval);
authRequestResponse = await this.authRequestApiService.postAdminAuthRequest(
this.authRequest,
);
const authRequestResponse = await this.authRequestApiService.postAdminAuthRequest(

Check warning on line 187 in libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts#L187

Added line #L187 was not covered by tests
this.authRequest,
);
const adminAuthReqStorable = new AdminAuthRequestStorable({

Check warning on line 190 in libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts#L190

Added line #L190 was not covered by tests
id: authRequestResponse.id,
privateKey: this.authRequestKeyPair.privateKey,
});

const adminAuthReqStorable = new AdminAuthRequestStorable({
id: authRequestResponse.id,
privateKey: this.authRequestKeyPair.privateKey,
});
const userId = (await firstValueFrom(this.accountService.activeAccount$)).id;
await this.authRequestService.setAdminAuthRequest(adminAuthReqStorable, userId);

Check warning on line 196 in libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts#L195-L196

Added lines #L195 - L196 were not covered by tests

const userId = (await firstValueFrom(this.accountService.activeAccount$)).id;
await this.authRequestService.setAdminAuthRequest(adminAuthReqStorable, userId);
} else {
await this.buildAuthRequest(AuthRequestType.AuthenticateAndUnlock);
authRequestResponse = await this.authRequestApiService.postAuthRequest(this.authRequest);
if (authRequestResponse.id) {
await this.anonymousHubService.createHubConnection(authRequestResponse.id);

Check warning on line 199 in libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts#L199

Added line #L199 was not covered by tests
}
} catch (e) {
this.logService.error(e);

Check warning on line 202 in libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts#L202

Added line #L202 was not covered by tests
}
}

protected async startStandardAuthRequestLogin(): Promise<void> {
this.showResendNotification = false;

Check warning on line 207 in libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts#L207

Added line #L207 was not covered by tests

try {
await this.buildAuthRequest(AuthRequestType.AuthenticateAndUnlock);

Check warning on line 210 in libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts#L209-L210

Added lines #L209 - L210 were not covered by tests

const authRequestResponse = await this.authRequestApiService.postAuthRequest(

Check warning on line 212 in libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts#L212

Added line #L212 was not covered by tests
this.authRequest,
);

if (authRequestResponse.id) {
await this.anonymousHubService.createHubConnection(authRequestResponse.id);

Check warning on line 217 in libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts#L217

Added line #L217 was not covered by tests
Expand Down Expand Up @@ -460,7 +486,7 @@ export class LoginViaAuthRequestComponent implements OnInit, OnDestroy {
await this.authRequestService.clearAdminAuthRequest(userId);

Check warning on line 486 in libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts#L486

Added line #L486 was not covered by tests

// start new auth request
await this.startAuthRequestLogin();
await this.startAdminAuthRequestLogin();

Check warning on line 489 in libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/auth/src/angular/login-via-auth-request/login-via-auth-request.component.ts#L489

Added line #L489 was not covered by tests
}

private async handlePostLoginNavigation(loginResponse: AuthResult) {
Expand Down

0 comments on commit dd610e3

Please sign in to comment.