Skip to content

Commit

Permalink
Merge branch 'master' into SWAP-4501-save-table-settings-in-user-exte…
Browse files Browse the repository at this point in the history
…rnal-settings
  • Loading branch information
martin-trajanovski authored Feb 27, 2025
2 parents c2e2951 + e08d6b1 commit da48496
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 7 deletions.
4 changes: 4 additions & 0 deletions cypress/e2e/datasets/datasets-general.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ describe("Datasets general", () => {

cy.finishedLoading();

cy.reload();
// Without reloading, the user will land on last visited page before logout
// i.e. the dataset detail page, because the login page "remembers" the previousRoute.

cy.url().should("include", "/login");

cy.get('mat-tab-group [role="tab"]').contains("Local").click();
Expand Down
3 changes: 2 additions & 1 deletion src/app/_layout/app-header/app-header.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ export class AppHeaderComponent implements OnInit {

login(): void {
if (this.config.skipSciCatLoginPageEnabled) {
const returnURL = encodeURIComponent(this.router.url);
for (const endpoint of this.oAuth2Endpoints) {
this.document.location.href = `${this.config.lbBaseURL}/${endpoint.authURL}`;
this.document.location.href = `${this.config.lbBaseURL}/${endpoint.authURL}?returnURL=${returnURL}`;
}
} else {
this.router.navigateByUrl("/login");
Expand Down
4 changes: 1 addition & 3 deletions src/app/app-routing/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ export class AuthGuard implements CanActivate {
.usersControllerGetMyUser()
.toPromise()
.catch(() => {
this.router.navigate(["/login"], {
queryParams: { returnUrl: state.url },
});
this.router.navigate(["/login"]);
return false;
})
.then(() => true);
Expand Down
7 changes: 7 additions & 0 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { CookieService } from "ngx-cookie-service";
import { TranslateLoader, TranslateModule } from "@ngx-translate/core";
import { CustomTranslateLoader } from "shared/loaders/custom-translate.loader";
import { DATE_PIPE_DEFAULT_OPTIONS } from "@angular/common";
import { RouteTrackerService } from "shared/services/route-tracker.service";

const appConfigInitializerFn = (appConfig: AppConfigService) => {
return () => appConfig.loadAppConfig();
Expand Down Expand Up @@ -103,6 +104,12 @@ const apiConfigurationFn = (
multi: true,
deps: [AppThemeService],
},
{
provide: APP_INITIALIZER,
useFactory: () => () => {},
multi: true,
deps: [RouteTrackerService],
},
{
provide: HTTP_INTERCEPTORS,
useClass: SnackbarInterceptor,
Expand Down
46 changes: 46 additions & 0 deletions src/app/shared/services/route-tracker.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { TestBed } from "@angular/core/testing";

import { RouteTrackerService } from "./route-tracker.service";
import { provideRouter, Router } from "@angular/router";
import { Component } from "@angular/core";

@Component({})
class DummyComponent {}

describe("RouteTrackerService", () => {
let service: RouteTrackerService;
let router: Router;

beforeEach(() => {
TestBed.configureTestingModule({
providers: [
provideRouter([
{ path: "datasets/:pid", component: DummyComponent },
{ path: "login", component: DummyComponent },
]),
],
});
service = TestBed.inject(RouteTrackerService);
router = TestBed.inject(Router);
});

it("should be created", () => {
expect(service).toBeTruthy();
});

it("should save the last route navigated from", async () => {
expect(router).toBeTruthy();
await router.navigateByUrl("/datasets/123");
await router.navigateByUrl("/login");
const previousRoute = service.getPreviousRoute();
expect(previousRoute).toBe("/datasets/123");
});

it("should preserve query parameters in the previous route", async () => {
expect(router).toBeTruthy();
await router.navigateByUrl("/datasets/123?query=value");
await router.navigateByUrl("/login");
const previousRoute = service.getPreviousRoute();
expect(previousRoute).toBe("/datasets/123?query=value");
});
});
24 changes: 24 additions & 0 deletions src/app/shared/services/route-tracker.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Injectable } from "@angular/core";
import { Router, NavigationStart } from "@angular/router";
import { filter } from "rxjs";

@Injectable({
providedIn: "root",
})
export class RouteTrackerService {
private currentRoute: string | null = null;
private previousRoute: string | null = null;

constructor(private router: Router) {
this.router.events
.pipe(filter((event) => event instanceof NavigationStart))
.subscribe((event) => {
this.previousRoute = this.currentRoute;
this.currentRoute = (event as NavigationStart).url;
});
}

getPreviousRoute(): string | null {
return this.previousRoute;
}
}
3 changes: 2 additions & 1 deletion src/app/users/auth-callback/auth-callback.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ export class AuthCallbackComponent implements OnInit {
);

// After the user is authenticated, we will redirect to the home page
const returnUrl = params["returnUrl"];
// or the value of returnUrl query param
const returnUrl: string = params["returnUrl"];
this.router.navigateByUrl(returnUrl || "/");
}
});
Expand Down
9 changes: 7 additions & 2 deletions src/app/users/login/login.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
AppConfigService,
OAuth2Endpoint,
} from "app-config.service";
import { RouteTrackerService } from "shared/services/route-tracker.service";

interface LoginForm {
username: string;
Expand Down Expand Up @@ -74,13 +75,17 @@ export class LoginComponent implements OnInit, OnDestroy {
private router: Router,
private route: ActivatedRoute,
private store: Store,
private routeTrackerService: RouteTrackerService,
@Inject(DOCUMENT) public document: Document,
) {
this.returnUrl = this.route.snapshot.queryParams["returnUrl"] || "";
this.returnUrl = this.routeTrackerService.getPreviousRoute() || "";
}

redirectOIDC(authURL: string) {
this.document.location.href = `${this.appConfig.lbBaseURL}/${authURL}`;
const returnURL = this.returnUrl
? encodeURIComponent(this.returnUrl)
: "/datasets";
this.document.location.href = `${this.appConfig.lbBaseURL}/${authURL}?returnURL=${returnURL}`;
}

openPrivacyDialog() {
Expand Down

0 comments on commit da48496

Please sign in to comment.