Skip to content

Commit 166cc0f

Browse files
committed
refactor(login): simplify navigation logic and improve code structure
1 parent c55d083 commit 166cc0f

2 files changed

Lines changed: 33 additions & 31 deletions

File tree

frontend/src/app/shared/components/auth/login/login.component.ts

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ export class LoginComponent implements OnInit {
5050
private modalService: NgbModal,
5151
private themeChangeBehavior: ThemeChangeBehavior,
5252
private spinner: NgxSpinnerService,
53-
private apiServiceCheckerService: ApiServiceCheckerService,
54-
private stateStorageService: StateStorageService
53+
private apiServiceCheckerService: ApiServiceCheckerService
5554
) {
5655
this.credentials = {};
5756
this.isInDemo = window.location.href.includes(DEMO_URL);
@@ -136,7 +135,6 @@ export class LoginComponent implements OnInit {
136135
this.logged = true;
137136
this.startLogin = false;
138137
this.spinner.show();
139-
this.startNavigation();
140138
} else if (data.tfaRequired && !!data.method ) {
141139
this.spinner.show();
142140
this.router.navigate(['/totp'])
@@ -170,25 +168,6 @@ export class LoginComponent implements OnInit {
170168
}
171169
}
172170

173-
startNavigation() {
174-
this.accountService.identity(true).then(account => {
175-
if (account) {
176-
const { path, queryParams } =
177-
extractQueryParamsForNavigation(this.stateStorageService.getUrl() ? this.stateStorageService.getUrl() : '' );
178-
if (path) {
179-
this.stateStorageService.resetPreviousUrl();
180-
}
181-
const redirectTo = (account.authorities.includes(ADMIN_ROLE) && account.email === ADMIN_DEFAULT_EMAIL)
182-
? '/getting-started' : !!path ? path : '/dashboard/overview';
183-
this.router.navigate([redirectTo], {queryParams})
184-
.then(() => this.spinner.hide());
185-
} else {
186-
this.logged = false;
187-
this.utmToast.showError('Login error', 'User without privileges.');
188-
}
189-
});
190-
}
191-
192171
startInternalNavigation(params) {
193172
if (params.url) {
194173
this.checkLogin(params.url);

frontend/src/app/shared/components/auth/totp/totp.component.ts

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ import {Component, OnInit} from '@angular/core';
22
import {DomSanitizer} from '@angular/platform-browser';
33
import {Router} from '@angular/router';
44
import {NgxSpinnerService} from 'ngx-spinner';
5-
import {Observable} from "rxjs";
5+
import {Observable} from 'rxjs';
66
import {AuthServerProvider} from '../../../../core/auth/auth-jwt.service';
7-
import {UtmToastService} from '../../../alert/utm-toast.service';
87
import {ThemeChangeBehavior} from '../../../behaviors/theme-change.behavior';
98
import {TfaMethod} from '../../../services/tfa/tfa.service';
9+
import {extractQueryParamsForNavigation} from "../../../util/query-params-to-filter.util";
10+
import {ADMIN_DEFAULT_EMAIL, ADMIN_ROLE} from "../../../constants/global.constant";
11+
import {StateStorageService} from "../../../../core/auth/state-storage.service";
12+
import {AccountService} from "../../../../core/auth/account.service";
13+
import {UtmToastService} from "../../../alert/utm-toast.service";
1014

1115

1216
@Component({
@@ -31,7 +35,10 @@ export class TotpComponent implements OnInit {
3135
private router: Router,
3236
private spinner: NgxSpinnerService,
3337
private themeChangeBehavior: ThemeChangeBehavior,
34-
public sanitizer: DomSanitizer) {
38+
public sanitizer: DomSanitizer,
39+
private stateStorageService: StateStorageService,
40+
private accountService: AccountService,
41+
private utmToast: UtmToastService) {
3542
}
3643

3744
ngOnInit(): void {
@@ -46,13 +53,10 @@ export class TotpComponent implements OnInit {
4653
.verifyCode(this.verificationCode).subscribe((auth) => {
4754
if (auth) {
4855
this.isVerified = true;
49-
this.spinner.show();
50-
this.router.navigate(['/dashboard/overview'])
51-
.then(() => this.spinner.hide());
56+
this.startNavigation();
5257
}
5358
}, error => {
5459
this.errorMessage = error.headers.get('X-UtmStack-error');
55-
console.log(error.headers.get('X-UtmStack-error'));
5660
this.isVerifying = false;
5761
});
5862
}
@@ -66,9 +70,28 @@ export class TotpComponent implements OnInit {
6670
}
6771

6872
clearError() {
69-
if(this.verificationCode.length == 6){
70-
this.onSubmit()
73+
if (this.verificationCode.length === 6) {
74+
this.onSubmit();
7175
}
7276
this.errorMessage = '';
7377
}
78+
79+
startNavigation() {
80+
this.accountService.identity(true).then(account => {
81+
if (account) {
82+
const { path, queryParams } =
83+
extractQueryParamsForNavigation(this.stateStorageService.getUrl() ? this.stateStorageService.getUrl() : '' );
84+
if (path) {
85+
this.stateStorageService.resetPreviousUrl();
86+
}
87+
const redirectTo = (account.authorities.includes(ADMIN_ROLE) && account.email === ADMIN_DEFAULT_EMAIL)
88+
? '/getting-started' : !!path ? path : '/dashboard/overview';
89+
console.log(redirectTo);
90+
this.router.navigate([redirectTo], {queryParams})
91+
.then(() => this.spinner.hide());
92+
} else {
93+
this.utmToast.showError('Login error', 'User without privileges.');
94+
}
95+
});
96+
}
7497
}

0 commit comments

Comments
 (0)