Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { LayoutModule } from "_layout/layout.module";
import { AppConfigService } from "app-config.service";
import { AppThemeService } from "app-theme.service";
import { SnackbarInterceptor } from "shared/interceptors/snackbar.interceptor";
import { AuthInterceptor } from "shared/interceptors/auth.interceptor";
import { AuthService } from "shared/services/auth/auth.service";
import { InternalStorage, SDKStorage } from "shared/services/auth/base.storage";
import { CookieService } from "ngx-cookie-service";
Expand Down Expand Up @@ -112,6 +113,11 @@ const apiConfigurationFn = (
useClass: SnackbarInterceptor,
multi: true,
},
{
provide: HTTP_INTERCEPTORS,
useClass: AuthInterceptor,
multi: true,
},
{
provide: MAT_FORM_FIELD_DEFAULT_OPTIONS,
useValue: {
Expand Down
45 changes: 45 additions & 0 deletions src/app/shared/interceptors/auth.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Injectable } from "@angular/core";
import {
HttpRequest,
HttpHandler,
HttpEvent,
HttpInterceptor,
HttpErrorResponse,
} from "@angular/common/http";
import { Observable, throwError } from "rxjs";
import { catchError } from "rxjs/operators";
import { Router } from "@angular/router";
import { NgZone } from "@angular/core";
import { Subscription } from "rxjs";
import { sessionTimeoutAction } from "state-management/actions/user.actions";
import { Store } from "@ngrx/store";
import { selectProfile } from "state-management/selectors/user.selectors";

@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor(
private router: Router,
private store: Store,
private zone: NgZone,
) {}

intercept(
request: HttpRequest<unknown>,
next: HttpHandler,
): Observable<HttpEvent<unknown>> {
return next.handle(request).pipe(
catchError((error: HttpErrorResponse) => {
const isSessionExpired =
error.status === 401 && error.error?.message === "SESSION_EXPIRED";

if (isSessionExpired) {
const currentUrl = this.router.url;
sessionStorage.setItem("postLoginRedirect", currentUrl);
this.store.dispatch(sessionTimeoutAction());
}
Comment thread
sourcery-ai[bot] marked this conversation as resolved.

return throwError(() => error);
}),
);
}
}
2 changes: 2 additions & 0 deletions src/app/state-management/actions/user.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ export const logoutCompleteAction = createAction(
);
export const logoutFailedAction = createAction("[User] Logout Failed");

export const sessionTimeoutAction = createAction("[User] Session Timeout");

export const addCustomColumnsAction = createAction(
"[User] Add Custom Columns",
props<{ names: string[]; scope: "dataset" | "sample" }>(),
Expand Down
21 changes: 21 additions & 0 deletions src/app/state-management/effects/user.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,27 @@ export class UserEffects {
);
});

sessionTimeout$ = createEffect(() => {
return this.actions$.pipe(
ofType(fromActions.sessionTimeoutAction),
filter(() => this.authService.isAuthenticated()),
switchMap(() => {
this.authService.clear();
return of(
clearDatasetsStateAction(),
clearInstrumentsStateAction(),
clearJobsStateAction(),
clearLogbooksStateAction(),
clearPoliciesStateAction(),
clearProposalsStateAction(),
clearPublishedDataStateAction(),
clearSamplesStateAction(),
fromActions.logoutCompleteAction({ logoutURL: "/login" }),
);
}),
);
});

logoutNavigate$ = createEffect(
() => {
return this.actions$.pipe(
Expand Down
7 changes: 7 additions & 0 deletions src/app/state-management/reducers/user.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ const reducer = createReducer(
}),
),

on(
fromActions.sessionTimeoutAction,
(): UserState => ({
...initialUserState,
}),
),

on(
fromActions.addCustomColumnsAction,
(state, { names, scope }): UserState => {
Expand Down
6 changes: 6 additions & 0 deletions src/app/users/login/login.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ export class LoginComponent implements OnInit, OnDestroy {
this.loginLocalEnabled = this.appConfig.loginLocalEnabled;
this.oAuth2Endpoints = this.appConfig.oAuth2Endpoints;

const storedRedirect = sessionStorage.getItem("postLoginRedirect");
if (storedRedirect) {
this.returnUrl = storedRedirect;
sessionStorage.removeItem("postLoginRedirect");
}

this.proceedSubscription = this.vm$
.pipe(filter((vm) => vm.isLoggedIn))
.subscribe(() => {
Expand Down
Loading