Skip to content

Commit

Permalink
new session checker component
Browse files Browse the repository at this point in the history
  • Loading branch information
alx652 committed May 25, 2023
1 parent 067191f commit 395d9c9
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/app/core/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ import { ElementLabelDisplayModule } from './utils/element-label-display.module'
import { UserQueryListDialogComponent } from '@gsrs-core/bulk-search/user-query-list-dialog/user-query-list-dialog.component';
import { ListCreateDialogComponent } from '@gsrs-core/substances-browse/list-create-dialog/list-create-dialog.component';
import { PrivacyStatementModule } from './privacy-statement/privacy-statement.module';
import { SessionCheckerComponent } from './auth/session-checker.component';
import { Location, LocationStrategy, PathLocationStrategy } from '@angular/common';

@NgModule({
declarations: [
Expand Down Expand Up @@ -154,7 +156,8 @@ import { PrivacyStatementModule } from './privacy-statement/privacy-statement.mo
RegisterComponent,
PwdRecoveryComponent,
UserQueryListDialogComponent,
ListCreateDialogComponent
ListCreateDialogComponent,
SessionCheckerComponent
],
imports: [
BrowserModule.withServerTransition({ appId: 'gsrs' }),
Expand Down Expand Up @@ -226,6 +229,9 @@ import { PrivacyStatementModule } from './privacy-statement/privacy-statement.mo
ElementLabelDisplayModule,
PrivacyStatementModule
],



providers: [
{
provide: ErrorHandler,
Expand All @@ -243,7 +249,7 @@ import { PrivacyStatementModule } from './privacy-statement/privacy-statement.mo
{ provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true },
{provide: WidgetRegistry, useClass: MyWidgetRegistry}
],
bootstrap: [AppComponent],
bootstrap: [AppComponent, Location],
entryComponents: [
HighlightedSearchActionComponent,
ExportDialogComponent,
Expand Down
1 change: 1 addition & 0 deletions src/app/core/auth/session-checker.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div>here I am in session checker 2</div>
5 changes: 5 additions & 0 deletions src/app/core/auth/session-checker.component.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


div {

}
107 changes: 107 additions & 0 deletions src/app/core/auth/session-checker.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { Component, Input, OnDestroy, OnInit, Output } from "@angular/core";
import { HttpClient } from '@angular/common/http';
import { of, Subscription, timer } from "rxjs";
import { catchError, filter, switchMap } from "rxjs/operators";
import { ConfigService } from '../config/config.service';
import { Router, ActivatedRoute, UrlSegment, NavigationEnd } from '@angular/router';
import {Location} from '@angular/common';

/*
Example configuration:
"sessionChecker": {
"check": false, # does nothing if false
"redirectUrl": "", # first priority handler action taken if not blank or undefined
"redirectToLogin": false, # 2nd priority handler, action take if true
"alertMessage": "" # 3rd priority handler, action taken if not blank or undefined
},
*/

@Component({
selector: "app-session-checker",
templateUrl: "./session-checker.component.html",
styleUrls: ["./session-checker.component.scss"]
})
export class SessionCheckerComponent implements OnInit, OnDestroy {
@Output() data: any;
apiUrl: string;
subscription: Subscription;

constructor(
private http: HttpClient,
public configService: ConfigService,
private router: Router,
private activatedRoute: ActivatedRoute,
private location: Location
) {}

ngOnInit() {
const config = this.configService?.configData;
const check = config?.sessionChecker?.check;
const interval: number = config?.sessionChecker?.interval||30;
const milliseconds: number= interval * 60 * 1000;
this.apiUrl =`${(config?.apiBaseUrl) || '/'}api/v1/whoami`;

if (config!==undefined && check && check===true) {
const path = this.location?.path();
console.log("here path "+ path);
if (path!==undefined && !(path=='/login' || path.match('^\/login\?'))) {

this.subscription = timer(0, milliseconds)
.pipe(
switchMap(() => {
return this.getData()
.pipe(catchError(err => {
this.handler();
console.error(err);
return of(undefined);
}));
}),
filter(data => data !== undefined)
)
.subscribe(data => {
this.data = data;
});
} // login path
} // config && check
}

ngOnDestroy() {
if(this.subscription!==undefined) {
this.subscription.unsubscribe();
}
}

handler() {
let actionTaken = false;
const redirectUrl = this.configService.configData?.sessionChecker?.redirectUrl||'';

if(redirectUrl) {
actionTaken = true;
window.location.href = this.configService.configData.sessionChecker.redirectUrl;
}
if (!actionTaken) {
// tried to make this more general but got infinite loops in some cases.
const redirectToLogin = this.configService.configData?.sessionChecker?.redirectToLogin||'';
if (redirectToLogin) {
actionTaken = true;
this.router.navigate(['/login']);
}
}

if(!actionTaken) {
const alertMessage = this.configService.configData?.sessionChecker?.alertMessage||'';
if (alertMessage) {
actionTaken = true;
alert(alertMessage)
}
}
if (!actionTaken) {
console.log("Session expired but session checker handler took no action. Check configration options.");
}

}

getData() {
return this.http.get(this.apiUrl);
}
}
3 changes: 2 additions & 1 deletion src/app/core/base/base.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,5 @@
</mat-toolbar>
<app-main-notification></app-main-notification>
<app-loading></app-loading>
<router-outlet></router-outlet>
<div *ngIf="sessionChecker"><app-session-checker></app-session-checker></div>
<router-outlet></router-outlet>
2 changes: 2 additions & 0 deletions src/app/core/base/base.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export class BaseComponent implements OnInit, OnDestroy {
private wildCardText: string;
private classicLinkQueryParams = {};
showHeaderBar = 'true';
sessionChecker = false;

constructor(
private router: Router,
Expand Down Expand Up @@ -145,6 +146,7 @@ export class BaseComponent implements OnInit, OnDestroy {
this.classicLinkQueryParamsString = '';
this.contactEmail = this.configService.configData.contactEmail || null;
this.navItems = this.configService.configData.navItems || null;
this.sessionChecker = this.configService.configData.sessionChecker?.check || null

let notempty = false;
if (this.loadedComponents) {
Expand Down
10 changes: 10 additions & 0 deletions src/app/core/config/config.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface Config {
apiSSG4mBaseUrl?: string;
apiUrlDomain?: string;
logoutRedirectUrl?: string;
sessionChecker?: any;
googleAnalyticsId?: string;
version?: string;
buildDateTime?: string;
Expand Down Expand Up @@ -55,7 +56,16 @@ export interface Config {
bulkSearch?: any
useDataUrl?: any;
}
export interface SessionChecker {
check?: boolean;
redirectUrl?: string;
redirectToLogin?: boolean;
alertMessage?: string;
interval?: number;



}
export interface LoadedComponents {
applications?: boolean;
products?: boolean;
Expand Down

0 comments on commit 395d9c9

Please sign in to comment.