-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add log off due to inactivity. (#2173)
- Loading branch information
1 parent
ea3184e
commit fbd7d9a
Showing
9 changed files
with
158 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
libs/dh/shared/feature-authorization/src/lib/dh-inactivity-detection.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* @license | ||
* Copyright 2020 Energinet DataHub A/S | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License2"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { Injectable } from '@angular/core'; | ||
import { | ||
concat, | ||
distinctUntilChanged, | ||
fromEvent, | ||
map, | ||
merge, | ||
of, | ||
startWith, | ||
switchMap, | ||
timer, | ||
} from 'rxjs'; | ||
import { WattModalService } from '@energinet-datahub/watt/modal'; | ||
import { DhInactivityLogoutComponent } from './dh-inactivity-logout.component'; | ||
import { MsalService } from '@azure/msal-angular'; | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class DhInactivityDetectionService { | ||
private readonly secondsUntilWarning = 115 * 60; | ||
|
||
private readonly inputDetection$ = merge( | ||
fromEvent(document, 'mousemove'), | ||
fromEvent(document, 'mousedown'), | ||
fromEvent(document, 'keydown'), | ||
fromEvent(document, 'wheel'), | ||
fromEvent(document, 'touchmove'), | ||
fromEvent(document, 'touchstart') | ||
); | ||
|
||
private readonly userInactive$ = this.inputDetection$.pipe( | ||
startWith(null), | ||
switchMap(() => concat(of(1), timer(this.secondsUntilWarning * 1000))), | ||
distinctUntilChanged(), | ||
map((isActive) => !isActive) | ||
); | ||
|
||
constructor( | ||
private readonly modalService: WattModalService, | ||
private readonly msal: MsalService | ||
) {} | ||
|
||
public trackInactivity() { | ||
this.userInactive$.subscribe((isInactive) => { | ||
if (isInactive) { | ||
this.modalService.open({ | ||
component: DhInactivityLogoutComponent, | ||
onClosed: (result) => result && this.msal.logout(), | ||
}); | ||
} else { | ||
this.modalService.close(false); | ||
} | ||
}); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
libs/dh/shared/feature-authorization/src/lib/dh-inactivity-logout.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* @license | ||
* Copyright 2020 Energinet DataHub A/S | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License2"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { Component } from '@angular/core'; | ||
import { CommonModule, DatePipe } from '@angular/common'; | ||
import { RxPush } from '@rx-angular/template/push'; | ||
import { map, take, tap, timer } from 'rxjs'; | ||
import { WATT_MODAL, WattDialogRef } from '@energinet-datahub/watt/modal'; | ||
import { TranslocoModule } from '@ngneat/transloco'; | ||
|
||
@Component({ | ||
selector: 'dh-inactivity-logout', | ||
styles: [ | ||
` | ||
h2 { | ||
text-align: center; | ||
} | ||
`, | ||
], | ||
template: ` | ||
<watt-modal *transloco="let t" [size]="'small'" [title]="t('sessionExpirationTitle')"> | ||
<p>{{ t('sessionExpirationContentPartA') }}<br />{{ t('sessionExpirationContentPartB') }}</p> | ||
<h2>{{ warningCountdown$ | push | date : 'mm:ss' }}</h2> | ||
</watt-modal> | ||
`, | ||
standalone: true, | ||
imports: [CommonModule, RxPush, DatePipe, TranslocoModule, WATT_MODAL], | ||
}) | ||
export class DhInactivityLogoutComponent { | ||
private readonly secondsUntilLogOff = 5 * 60; | ||
|
||
readonly warningCountdown$ = timer(0, 1000).pipe( | ||
take(this.secondsUntilLogOff + 1), | ||
tap((elapsed) => elapsed >= this.secondsUntilLogOff && this.dialogRef.close(true)), | ||
map((elapsed) => Math.max(0, this.secondsUntilLogOff - elapsed - 1)), | ||
map((elapsed) => new Date(elapsed * 1000)) | ||
); | ||
|
||
constructor(private dialogRef: WattDialogRef<DhInactivityLogoutComponent>) {} | ||
} |