From 06a50defb4409e6a7119e57c1b9b18609a6b05f1 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 --- Open-ILS/src/eg2/src/app/core/auth.service.ts | 3 -- Open-ILS/src/eg2/src/app/staff/login.component.ts | 36 +++++++++++++--------- 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, 27 insertions(+), 22 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 d9b9621d04..57d03dd1ae 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 @@ -92,15 +102,11 @@ export class StaffLoginComponent implements OnInit { // and user/workstation setting values this.org.clearCachedSettings().then(_ => { - // Force reload of the app after a successful login. - // This allows the route resolver to re-run with a - // valid auth token and workstation. - - // Temporarily redirect to AngularJS splash page - // (LP#1848550/LP#1835128) - window.location.href = '/eg/staff/splash'; - // this.ngLocation.prepareExternalUrl(url); - + // Force reload of the app via full navigation + // (instead of route change) after a successful + // login. This allows the route resolver + // to re-run with a valid auth token and workstation. + window.location.href = 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 a627a444a8..de6252b059 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