From 6419b91c6d6f40a451564f694410fbffd9f15a9f Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Thu, 18 Feb 2021 12:05:37 -0500 Subject: [PATCH] LP1904036 Angular login now handles routeTo Signed-off-by: Bill Erickson Signed-off-by: Jane Sandberg Signed-off-by: Galen Charlton --- Open-ILS/src/eg2/src/app/core/auth.service.ts | 3 --- Open-ILS/src/eg2/src/app/staff/login.component.ts | 23 +++++++++++++++------- Open-ILS/src/eg2/src/app/staff/resolver.service.ts | 4 ++-- Open-ILS/src/eg2/src/app/staff/staff.component.ts | 6 ++++-- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/Open-ILS/src/eg2/src/app/core/auth.service.ts b/Open-ILS/src/eg2/src/app/core/auth.service.ts index 9ad471fb02..7a04f5874a 100644 --- a/Open-ILS/src/eg2/src/app/core/auth.service.ts +++ b/Open-ILS/src/eg2/src/app/core/auth.service.ts @@ -46,9 +46,6 @@ export class AuthService { workstationState: AuthWsState = AuthWsState.PENDING; - // Used by auth-checking resolvers - redirectUrl: string; - // reference to active auth validity setTimeout handler. pollTimeout: any; diff --git a/Open-ILS/src/eg2/src/app/staff/login.component.ts b/Open-ILS/src/eg2/src/app/staff/login.component.ts index 48ce941977..f78991fcf4 100644 --- a/Open-ILS/src/eg2/src/app/staff/login.component.ts +++ b/Open-ILS/src/eg2/src/app/staff/login.component.ts @@ -5,14 +5,17 @@ import {AuthService, AuthWsState} from '@eg/core/auth.service'; import {StoreService} from '@eg/core/store.service'; import {OrgService} from '@eg/core/org.service'; +// Direct users to the AngJS splash page when no routeTo is provided. +const SPLASH_PAGE_PATH = '/eg/staff/splash'; + @Component({ templateUrl : './login.component.html' }) - export class StaffLoginComponent implements OnInit { workstations: any[]; loginFailed: boolean; + routeTo: string; args = { username : '', @@ -32,6 +35,16 @@ export class StaffLoginComponent implements OnInit { ) {} ngOnInit() { + this.routeTo = this.route.snapshot.queryParamMap.get('routeTo'); + + if (this.routeTo) { + if (this.routeTo.match(/^[a-z]+:\/\//i)) { + console.warn( + 'routeTo must contain only path information: ', this.routeTo); + this.routeTo = null; + } + } + // clear out any stale auth data this.auth.logout(); @@ -63,19 +76,16 @@ export class StaffLoginComponent implements OnInit { handleSubmit() { // post-login URL - let url: string = this.auth.redirectUrl || '/staff/splash'; + let url: string = this.routeTo || SPLASH_PAGE_PATH; // prevent sending the user back to the login page - if (url.startsWith('/staff/login')) { - url = '/staff/splash'; - } + if (url.match('/staff/login')) { url = SPLASH_PAGE_PATH; } const workstation: string = this.args.workstation; this.loginFailed = false; this.auth.login(this.args).then( ok => { - this.auth.redirectUrl = null; if (this.auth.workstationState === AuthWsState.NOT_FOUND_SERVER) { // User attempted to login with a workstation that is @@ -97,7 +107,6 @@ export class StaffLoginComponent implements OnInit { // valid auth token and workstation. window.location.href = this.ngLocation.prepareExternalUrl(url); - }); } }, diff --git a/Open-ILS/src/eg2/src/app/staff/resolver.service.ts b/Open-ILS/src/eg2/src/app/staff/resolver.service.ts index 0ce843cfd4..2c3a7746fd 100644 --- a/Open-ILS/src/eg2/src/app/staff/resolver.service.ts +++ b/Open-ILS/src/eg2/src/app/staff/resolver.service.ts @@ -110,8 +110,8 @@ export class StaffResolver implements Resolve> { // valid auth token. Send the caller back to the login page. handleInvalidToken(state: RouterStateSnapshot): void { console.debug('StaffResolver: authtoken is not valid'); - this.auth.redirectUrl = state.url; - this.router.navigate([LOGIN_PATH]); + const url = this.ngLocation.prepareExternalUrl(state.url); + this.router.navigate([LOGIN_PATH], {queryParams: {routeTo: url}}); this.observer.error('invalid or no auth token'); } diff --git a/Open-ILS/src/eg2/src/app/staff/staff.component.ts b/Open-ILS/src/eg2/src/app/staff/staff.component.ts index 952a46852d..ac075e39d9 100644 --- a/Open-ILS/src/eg2/src/app/staff/staff.component.ts +++ b/Open-ILS/src/eg2/src/app/staff/staff.component.ts @@ -1,4 +1,5 @@ import {Component, OnInit, NgZone, HostListener} from '@angular/core'; +import {Location} from '@angular/common'; import {Router, ActivatedRoute, NavigationEnd} from '@angular/router'; import {AuthService, AuthWsState} from '@eg/core/auth.service'; import {NetService} from '@eg/core/net.service'; @@ -19,6 +20,7 @@ export class StaffComponent implements OnInit { constructor( private router: Router, private route: ActivatedRoute, + private ngLocation: Location, private zone: NgZone, private net: NetService, private auth: AuthService, @@ -46,7 +48,7 @@ export class StaffComponent implements OnInit { } console.debug('Auth session has expired. Redirecting to login'); - this.auth.redirectUrl = this.router.url; + const url = this.ngLocation.prepareExternalUrl(this.router.url); // https://github.com/angular/angular/issues/18254 // When a tab redirects to a login page as a result of @@ -55,7 +57,7 @@ export class StaffComponent implements OnInit { // with the page. Fix it by wrapping it in zone.run(). // This is the only navigate() where I have seen this happen. this.zone.run(() => { - this.router.navigate([LOGIN_PATH]); + this.router.navigate([LOGIN_PATH], {queryParams: {routeTo: url}}); }); }); -- 2.11.0