From 524989905c0ab389cfc9385fe99ddfb34153c393 Mon Sep 17 00:00:00 2001 From: Chneemann Date: Sat, 25 May 2024 19:36:21 +0200 Subject: [PATCH] added session logout timer --- src/app/app.component.ts | 14 ++++++++++++++ .../contact-form/contact-form.component.html | 3 ++- src/app/services/login.service.ts | 7 ++++--- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 6fccb2d..aab5a7e 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -44,6 +44,7 @@ export class AppComponent { private route: ActivatedRoute, private router: Router ) { + this.checkAndClearLocalStorage(); this.isLoggedIn = this.firebaseService.getCurrentUserId(); } @@ -56,6 +57,19 @@ export class AppComponent { } } + checkAndClearLocalStorage() { + const startTime = localStorage.getItem('sessionTimeJOIN'); + + if (startTime) { + const startTimeMillis = parseInt(startTime); + const currentTime = new Date().getTime(); + const timeDifference = 12 * 60 * 60 * 1000; // 12h + if (currentTime - startTimeMillis > timeDifference) { + localStorage.clear(); + } + } + } + checkPwResetRoute() { this.router.events .pipe(filter((event) => event instanceof NavigationEnd)) diff --git a/src/app/components/contacts/contact-edit-new/contact-form/contact-form.component.html b/src/app/components/contacts/contact-edit-new/contact-form/contact-form.component.html index 24b6ddd..916b2db 100644 --- a/src/app/components/contacts/contact-edit-new/contact-form/contact-form.component.html +++ b/src/app/components/contacts/contact-edit-new/contact-form/contact-form.component.html @@ -121,7 +121,8 @@ }
- @if (currentUserId){ + @if (currentUserId && currentUserId !== + firebaseService.getCurrentUserId()){ 0 && userData[0].id) { - this.getUserIdInLocalStorage(this.secretKey, userData[0].id); + this.getUserIdInLocalStorage(userData[0].id); this.updateUserOnlineStatus(userData[0].id, true); } }) @@ -161,7 +161,7 @@ export class LoginService { ifExistUser(user: string) { const userData = this.firebaseService.getUserDataFromUid(user); if (userData.length > 0 && userData[0].id) { - this.getUserIdInLocalStorage(this.secretKey, userData[0].id); + this.getUserIdInLocalStorage(userData[0].id); this.updateUserOnlineStatus(userData[0].id, true); } } @@ -187,12 +187,13 @@ export class LoginService { : './../../../assets/img/login/close-eye.svg'; } - getUserIdInLocalStorage(key: string, userId: string): void { + getUserIdInLocalStorage(userId: string): void { const encryptedValue = CryptoJS.AES.encrypt( JSON.stringify(userId), this.secretKey ).toString(); localStorage.setItem('currentUserJOIN', encryptedValue); + localStorage.setItem('sessionTimeJOIN', new Date().getTime().toString()); } // LOGOUT