LP1904036 Angular login now handles routeTo
authorBill Erickson <berickxx@gmail.com>
Thu, 18 Feb 2021 17:05:37 +0000 (12:05 -0500)
committerGalen Charlton <gmc@equinoxOLI.org>
Fri, 28 Oct 2022 00:13:23 +0000 (20:13 -0400)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Signed-off-by: Jane Sandberg <js7389@princeton.edu>
Signed-off-by: Galen Charlton <gmc@equinoxOLI.org>
Open-ILS/src/eg2/src/app/core/auth.service.ts
Open-ILS/src/eg2/src/app/staff/login.component.ts
Open-ILS/src/eg2/src/app/staff/resolver.service.ts
Open-ILS/src/eg2/src/app/staff/staff.component.ts

index 9ad471f..7a04f58 100644 (file)
@@ -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;
 
index 48ce941..f78991f 100644 (file)
@@ -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);
-
                     });
                 }
             },
index 0ce843c..2c3a774 100644 (file)
@@ -110,8 +110,8 @@ export class StaffResolver implements Resolve<Observable<any>> {
     // 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');
     }
 
index 952a468..ac075e3 100644 (file)
@@ -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}});
             });
         });