diff --git a/src/app/content/hemat-app/cost-management/cost-management.component.ts b/src/app/content/hemat-app/cost-management/cost-management.component.ts index c72d750..86f5264 100644 --- a/src/app/content/hemat-app/cost-management/cost-management.component.ts +++ b/src/app/content/hemat-app/cost-management/cost-management.component.ts @@ -54,7 +54,8 @@ export class CostManagementComponent implements OnInit { ) {} ngOnInit() { - this.authService.checkTokenAndRedirect(); + this.authService.startTokenCheck(); + this.authService.startTrackingActivity(); this.breadcrumb = { mainlabel: "Cost Management", links: [ diff --git a/src/app/content/hemat-app/cost-management/modal-add-actual/modal-add-actual.component.ts b/src/app/content/hemat-app/cost-management/modal-add-actual/modal-add-actual.component.ts index 94a5360..b8ddb94 100644 --- a/src/app/content/hemat-app/cost-management/modal-add-actual/modal-add-actual.component.ts +++ b/src/app/content/hemat-app/cost-management/modal-add-actual/modal-add-actual.component.ts @@ -30,7 +30,8 @@ export class ModalAddActualComponent { } ngOnInit() { - this.authService.checkTokenAndRedirect(); + this.authService.startTokenCheck(); + this.authService.startTrackingActivity(); const currentDate = new Date(); this.dateCurrent = currentDate.toISOString().slice(0, 7); this.dateFormat(); diff --git a/src/app/content/hemat-app/cost-management/modal-export/modal-export.component.ts b/src/app/content/hemat-app/cost-management/modal-export/modal-export.component.ts index 27ad24d..a6434b1 100644 --- a/src/app/content/hemat-app/cost-management/modal-export/modal-export.component.ts +++ b/src/app/content/hemat-app/cost-management/modal-export/modal-export.component.ts @@ -30,7 +30,8 @@ export class ModalExportComponent { ) {} ngOnInit() { - this.authService.checkTokenAndRedirect(); + this.authService.startTokenCheck(); + this.authService.startTrackingActivity(); const dateRow = this.convertToUTC7(this.dataRow.endDate) this.formattedEndDate = dateRow.slice(0, 7); } diff --git a/src/app/content/hemat-app/device/add-edit-device/add-edit-device.component.ts b/src/app/content/hemat-app/device/add-edit-device/add-edit-device.component.ts index 2908c9c..7b53b3d 100644 --- a/src/app/content/hemat-app/device/add-edit-device/add-edit-device.component.ts +++ b/src/app/content/hemat-app/device/add-edit-device/add-edit-device.component.ts @@ -41,7 +41,8 @@ export class AddEditDeviceComponent implements OnInit{ ) {} ngOnInit() { - this.authService.checkTokenAndRedirect(); + this.authService.startTokenCheck(); + this.authService.startTrackingActivity(); this.route.params.subscribe(params => { const id = params['id']; this.deviceId = id; @@ -79,27 +80,27 @@ export class AddEditDeviceComponent implements OnInit{ const dataCategory = data.data.find( (item) => item.name === "master_category" ).headerDetailParam; - this.dataMasterCategori = dataCategory.filter(item => item.statusName.toLowerCase() === "aktif") + this.dataMasterCategori = dataCategory.filter(item => item.statusName.toLowerCase() === "aktif" || item.status.toLowerCase() === "71") const dataVoltage = data.data.find( (item) => item.name === "master_voltage" ).headerDetailParam; - this.dataMasterVoltage = dataVoltage.filter(item => item.statusName.toLowerCase() === "aktif") + this.dataMasterVoltage = dataVoltage.filter(item => item.statusName.toLowerCase() === "aktif" || item.status.toLowerCase() === "71") const dataType = data.data.find( (item) => item.name === "master_type" ).headerDetailParam; - this.dataMasterType = dataType.filter(item => item.statusName.toLowerCase() === "aktif") + this.dataMasterType = dataType.filter(item => item.statusName.toLowerCase() === "aktif" || item.status.toLowerCase() === "71") const dataDuration = data.data.find( (item) => item.name === "master_duration" ).headerDetailParam; - this.dataMasterDuration = dataDuration.filter(item => item.statusName.toLowerCase() === "aktif") + this.dataMasterDuration = dataDuration.filter(item => item.statusName.toLowerCase() === "aktif" || item.status.toLowerCase() === "71") const dataStatus = data.data.find( (item) => item.name === "master_status" ).headerDetailParam; - this.dataMasterStatus = dataStatus.filter(item => item.statusName.toLowerCase() === "aktif") + this.dataMasterStatus = dataStatus.filter(item => item.statusName.toLowerCase() === "aktif" || item.status.toLowerCase() === "71") }); } diff --git a/src/app/content/hemat-app/device/device-control/device-control.component.ts b/src/app/content/hemat-app/device/device-control/device-control.component.ts index a32bca8..39f2cf7 100644 --- a/src/app/content/hemat-app/device/device-control/device-control.component.ts +++ b/src/app/content/hemat-app/device/device-control/device-control.component.ts @@ -38,7 +38,8 @@ export class DeviceControlComponent { ) {} ngOnInit() { - this.authService.checkTokenAndRedirect(); + this.authService.startTokenCheck(); + this.authService.startTrackingActivity(); this.breadcrumb = { mainlabel: "Device", links: [ diff --git a/src/app/content/hemat-app/device/device.component.ts b/src/app/content/hemat-app/device/device.component.ts index 0b00886..869ce5a 100644 --- a/src/app/content/hemat-app/device/device.component.ts +++ b/src/app/content/hemat-app/device/device.component.ts @@ -53,7 +53,8 @@ export class DeviceComponent implements OnInit { ) {} ngOnInit() { - this.authService.checkTokenAndRedirect(); + this.authService.startTokenCheck(); + this.authService.startTrackingActivity(); this.breadcrumb = { mainlabel: "Device", links: [ diff --git a/src/app/content/hemat-app/list-monitoring/add-edit-list/add-edit-list.component.ts b/src/app/content/hemat-app/list-monitoring/add-edit-list/add-edit-list.component.ts index e7cce4a..9deace6 100644 --- a/src/app/content/hemat-app/list-monitoring/add-edit-list/add-edit-list.component.ts +++ b/src/app/content/hemat-app/list-monitoring/add-edit-list/add-edit-list.component.ts @@ -41,7 +41,8 @@ export class AddEditListComponent { ) {} ngOnInit() { - this.authService.checkTokenAndRedirect(); + this.authService.startTokenCheck(); + this.authService.startTrackingActivity(); this.route.data.subscribe((data) => { this.mode = data.mode; }); @@ -211,7 +212,7 @@ export class AddEditListComponent { const dataStatus = data.data.find( (item) => item.name === "master_status" ).headerDetailParam; - this.dataMasterStatus = dataStatus.filter(item => item.statusName.toLowerCase() === "aktif") + this.dataMasterStatus = dataStatus.filter(item => item.statusName.toLowerCase() === "aktif" || item.status.toLowerCase() === "71") }); } diff --git a/src/app/content/hemat-app/list-monitoring/list-monitoring.component.ts b/src/app/content/hemat-app/list-monitoring/list-monitoring.component.ts index 22fc841..513caab 100644 --- a/src/app/content/hemat-app/list-monitoring/list-monitoring.component.ts +++ b/src/app/content/hemat-app/list-monitoring/list-monitoring.component.ts @@ -25,7 +25,8 @@ export class ListMonitoringComponent { ) {} ngOnInit() { - this.authService.checkTokenAndRedirect(); + this.authService.startTokenCheck(); + this.authService.startTrackingActivity(); this.breadcrumb = { mainlabel: "Cost Management", links: [ diff --git a/src/app/content/hemat-app/master/add-edit-master/add-edit-master.component.ts b/src/app/content/hemat-app/master/add-edit-master/add-edit-master.component.ts index 9719184..fb685a8 100644 --- a/src/app/content/hemat-app/master/add-edit-master/add-edit-master.component.ts +++ b/src/app/content/hemat-app/master/add-edit-master/add-edit-master.component.ts @@ -27,7 +27,8 @@ export class AddEditMasterComponent implements OnInit { ) {} ngOnInit() { - this.authService.checkTokenAndRedirect(); + this.authService.startTokenCheck(); + this.authService.startTrackingActivity(); this.createForm(); this.dataListMaster(); if (this.mode === "add") { @@ -78,7 +79,7 @@ export class AddEditMasterComponent implements OnInit { const dataCategory = data.data.find( (item) => item.name === "master_status" ).headerDetailParam; - this.dataMasterStatus = dataCategory.filter(item => item.statusName.toLowerCase() === "aktif"); + this.dataMasterStatus = dataCategory.filter(item => item.statusName.toLowerCase() === "aktif" || item.status.toLowerCase() === "71" || item.status.toLowerCase() === "71"); }); } diff --git a/src/app/content/hemat-app/master/master-building/add-edit-master-building/add-edit-master-building.component.ts b/src/app/content/hemat-app/master/master-building/add-edit-master-building/add-edit-master-building.component.ts index 44fc668..6b339ee 100644 --- a/src/app/content/hemat-app/master/master-building/add-edit-master-building/add-edit-master-building.component.ts +++ b/src/app/content/hemat-app/master/master-building/add-edit-master-building/add-edit-master-building.component.ts @@ -27,7 +27,8 @@ export class AddEditMasterBuildingComponent { ) {} ngOnInit() { - this.authService.checkTokenAndRedirect(); + this.authService.startTokenCheck(); + this.authService.startTrackingActivity(); this.createForm(); this.listDataStatus(); if (this.mode === "add") { diff --git a/src/app/content/hemat-app/master/master-building/master-building.component.ts b/src/app/content/hemat-app/master/master-building/master-building.component.ts index 5c35d08..f634bba 100644 --- a/src/app/content/hemat-app/master/master-building/master-building.component.ts +++ b/src/app/content/hemat-app/master/master-building/master-building.component.ts @@ -30,7 +30,8 @@ export class MasterBuildingComponent { ) {} ngOnInit() { - this.authService.checkTokenAndRedirect(); + this.authService.startTokenCheck(); + this.authService.startTrackingActivity(); this.breadcrumb = { mainlabel: "Master Building", links: [ diff --git a/src/app/content/hemat-app/master/master-category/master-category.component.ts b/src/app/content/hemat-app/master/master-category/master-category.component.ts index 55228ff..24b1726 100644 --- a/src/app/content/hemat-app/master/master-category/master-category.component.ts +++ b/src/app/content/hemat-app/master/master-category/master-category.component.ts @@ -33,7 +33,6 @@ export class MasterCategoryComponent implements OnInit { ) {} ngOnInit() { - // this.authService.checkTokenAndRedirect(); this.authService.startTokenCheck(); this.authService.startTrackingActivity(); this.breadcrumb = { diff --git a/src/app/content/hemat-app/master/master-duration-use/master-duration-use.component.ts b/src/app/content/hemat-app/master/master-duration-use/master-duration-use.component.ts index 88148f1..1f5c87f 100644 --- a/src/app/content/hemat-app/master/master-duration-use/master-duration-use.component.ts +++ b/src/app/content/hemat-app/master/master-duration-use/master-duration-use.component.ts @@ -30,7 +30,6 @@ export class MasterDurationUseComponent { ) {} ngOnInit() { - // this.authService.checkTokenAndRedirect(); this.authService.startTokenCheck(); this.authService.startTrackingActivity(); this.breadcrumb = { diff --git a/src/app/content/hemat-app/master/master-floor/master-floor.component.ts b/src/app/content/hemat-app/master/master-floor/master-floor.component.ts index 261f7df..83f4752 100644 --- a/src/app/content/hemat-app/master/master-floor/master-floor.component.ts +++ b/src/app/content/hemat-app/master/master-floor/master-floor.component.ts @@ -30,7 +30,8 @@ export class MasterFloorComponent { ) {} ngOnInit() { - this.authService.checkTokenAndRedirect(); + this.authService.startTokenCheck(); + this.authService.startTrackingActivity(); this.breadcrumb = { mainlabel: "Master Floor", links: [ diff --git a/src/app/content/hemat-app/master/master-role/master-role.component.ts b/src/app/content/hemat-app/master/master-role/master-role.component.ts index 34a080e..5a5c149 100644 --- a/src/app/content/hemat-app/master/master-role/master-role.component.ts +++ b/src/app/content/hemat-app/master/master-role/master-role.component.ts @@ -30,7 +30,8 @@ export class MasterRoleComponent { ) {} ngOnInit() { - this.authService.checkTokenAndRedirect(); + this.authService.startTokenCheck(); + this.authService.startTrackingActivity(); this.breadcrumb = { mainlabel: "Master Role", links: [ diff --git a/src/app/content/hemat-app/master/master-room/add-edit-master-room/add-edit-master-room.component.ts b/src/app/content/hemat-app/master/master-room/add-edit-master-room/add-edit-master-room.component.ts index bd87287..a7b300b 100644 --- a/src/app/content/hemat-app/master/master-room/add-edit-master-room/add-edit-master-room.component.ts +++ b/src/app/content/hemat-app/master/master-room/add-edit-master-room/add-edit-master-room.component.ts @@ -30,7 +30,8 @@ export class AddEditMasterRoomComponent { ) {} ngOnInit() { - this.authService.checkTokenAndRedirect(); + this.authService.startTokenCheck(); + this.authService.startTrackingActivity(); this.createForm(); this.listDataStatus(); this.listDataBuilding(); diff --git a/src/app/content/hemat-app/master/master-room/master-room.component.ts b/src/app/content/hemat-app/master/master-room/master-room.component.ts index ca7be15..d1b203b 100644 --- a/src/app/content/hemat-app/master/master-room/master-room.component.ts +++ b/src/app/content/hemat-app/master/master-room/master-room.component.ts @@ -27,7 +27,8 @@ export class MasterRoomComponent { ) {} ngOnInit() { - this.authService.checkTokenAndRedirect(); + this.authService.startTokenCheck(); + this.authService.startTrackingActivity(); this.breadcrumb = { mainlabel: "Master Room", links: [ diff --git a/src/app/content/hemat-app/master/master-status/master-status.component.ts b/src/app/content/hemat-app/master/master-status/master-status.component.ts index d241e84..68ec9d5 100644 --- a/src/app/content/hemat-app/master/master-status/master-status.component.ts +++ b/src/app/content/hemat-app/master/master-status/master-status.component.ts @@ -30,7 +30,8 @@ export class MasterStatusComponent { ) {} ngOnInit() { - this.authService.checkTokenAndRedirect(); + this.authService.startTokenCheck(); + this.authService.startTrackingActivity(); this.breadcrumb = { mainlabel: "Master Status", links: [ diff --git a/src/app/content/hemat-app/master/master-type/master-type.component.ts b/src/app/content/hemat-app/master/master-type/master-type.component.ts index 7912de9..c697171 100644 --- a/src/app/content/hemat-app/master/master-type/master-type.component.ts +++ b/src/app/content/hemat-app/master/master-type/master-type.component.ts @@ -30,7 +30,8 @@ export class MasterTypeComponent { ) {} ngOnInit() { - this.authService.checkTokenAndRedirect(); + this.authService.startTokenCheck(); + this.authService.startTrackingActivity(); this.breadcrumb = { mainlabel: "Master Type", links: [ diff --git a/src/app/content/hemat-app/master/master-voltage/master-voltage.component.ts b/src/app/content/hemat-app/master/master-voltage/master-voltage.component.ts index 598977e..69cd16c 100644 --- a/src/app/content/hemat-app/master/master-voltage/master-voltage.component.ts +++ b/src/app/content/hemat-app/master/master-voltage/master-voltage.component.ts @@ -30,7 +30,8 @@ export class MasterVoltageComponent { ) {} ngOnInit() { - this.authService.checkTokenAndRedirect(); + this.authService.startTokenCheck(); + this.authService.startTrackingActivity(); this.breadcrumb = { mainlabel: "Master Voltage", links: [ diff --git a/src/app/content/hemat-app/monitoring/building/building.component.ts b/src/app/content/hemat-app/monitoring/building/building.component.ts index bb5340f..c689366 100644 --- a/src/app/content/hemat-app/monitoring/building/building.component.ts +++ b/src/app/content/hemat-app/monitoring/building/building.component.ts @@ -27,7 +27,8 @@ export class BuildingComponent { ) {} ngOnInit() { - this.authService.checkTokenAndRedirect(); + this.authService.startTokenCheck(); + this.authService.startTrackingActivity(); this.breadcrumb = { mainlabel: "Dashboard", links: [ diff --git a/src/app/content/hemat-app/monitoring/control-device-seemore/control-device-seemore.component.ts b/src/app/content/hemat-app/monitoring/control-device-seemore/control-device-seemore.component.ts index 39f46af..48917a1 100644 --- a/src/app/content/hemat-app/monitoring/control-device-seemore/control-device-seemore.component.ts +++ b/src/app/content/hemat-app/monitoring/control-device-seemore/control-device-seemore.component.ts @@ -37,7 +37,8 @@ export class ControlDeviceSeemoreComponent { ) {} ngOnInit() { - this.authService.checkTokenAndRedirect(); + this.authService.startTokenCheck(); + this.authService.startTrackingActivity(); this.route.params.subscribe((params) => { const buildingId = params["id"]; this.paramsId = buildingId ? buildingId: 0; diff --git a/src/app/content/hemat-app/monitoring/detail/detail.component.ts b/src/app/content/hemat-app/monitoring/detail/detail.component.ts index 860ebbc..7c7968c 100644 --- a/src/app/content/hemat-app/monitoring/detail/detail.component.ts +++ b/src/app/content/hemat-app/monitoring/detail/detail.component.ts @@ -140,7 +140,8 @@ export class DetailComponent { } ngOnInit() { - this.authService.checkTokenAndRedirect(); + this.authService.startTokenCheck(); + this.authService.startTrackingActivity(); this.route.data.subscribe((data) => { this.mode = data.mode; }); diff --git a/src/app/content/hemat-app/monitoring/room/room.component.ts b/src/app/content/hemat-app/monitoring/room/room.component.ts index 4eb4f62..a83c0da 100644 --- a/src/app/content/hemat-app/monitoring/room/room.component.ts +++ b/src/app/content/hemat-app/monitoring/room/room.component.ts @@ -25,7 +25,8 @@ export class RoomComponent implements OnInit { ) {} ngOnInit() { - this.authService.checkTokenAndRedirect(); + this.authService.startTokenCheck(); + this.authService.startTrackingActivity(); this.route.data.subscribe((data) => { this.mode = data.mode; console.log(this.mode); diff --git a/src/app/content/hemat-app/service/login.service.ts b/src/app/content/hemat-app/service/login.service.ts index 809ad66..59234e6 100644 --- a/src/app/content/hemat-app/service/login.service.ts +++ b/src/app/content/hemat-app/service/login.service.ts @@ -1,8 +1,16 @@ // src/app/services/login.service.ts import { Injectable, NgZone } from "@angular/core"; import { HttpClient, HttpHeaders } from "@angular/common/http"; -import { BehaviorSubject, Observable, of, Subject, timer, fromEvent, merge } from "rxjs"; -import {jwtDecode} from "jwt-decode"; +import { + BehaviorSubject, + Observable, + of, + Subject, + timer, + fromEvent, + merge, +} from "rxjs"; +import { jwtDecode } from "jwt-decode"; import { Router } from "@angular/router"; import { switchMap, debounceTime, mapTo, startWith } from "rxjs/operators"; import { AuthService } from "src/app/_services/auth.service"; @@ -93,11 +101,11 @@ export class LoginService { const tokenData = localStorage.getItem(this.tokenKey); if (tokenData) { const tokenInfo = JSON.parse(tokenData); - const decodedToken = jwtDecode(tokenInfo.refresh_token); + const decodedToken = jwtDecode(tokenInfo.access_token); const expiryDate = decodedToken.exp * 1000; const now = new Date().getTime(); const timeLeft = expiryDate - now; - return of(timeLeft <= 5 * 60 * 1000); + return of(timeLeft <= 2 * 60 * 1000); } return of(true); } @@ -108,12 +116,14 @@ export class LoginService { this.authService.doLogout().then( () => { this.router.navigate(["/login"]); + window.location.reload(); }, (err) => { console.log(err); } ); this.router.navigate(["/login"]); + window.location.reload(); } else { console.log("Token is valid, continuing..."); } @@ -121,30 +131,44 @@ export class LoginService { } startTokenCheck(): void { - timer(0, 2 * 60 * 1000) // Check every 5 minutes - .pipe( - switchMap(() => this.isTokenExpired()) - ) + timer(0, 2 * 60 * 1000) // Check every 5 minutes + .pipe(switchMap(() => this.isTokenExpired())) .subscribe((isExpired) => { + // console.log(isExpired); + if (isExpired) { - this.activity$.subscribe(isActive => { + this.activity$.subscribe((isActive) => { + // console.log(isActive); + if (!isActive) { this.checkTokenAndRedirect(); } else { - console.log("Token expired but user is active. Refresh token not implemented."); - this.updateUserProfile(this.currentUser.refresh_token) - .subscribe((resp) => { - const decodedToken = jwtDecode(resp.access_token); - localStorage.setItem("account_info", JSON.stringify(decodedToken)); - const userProfile = { - access_token: resp.access_token, - refresh_token: resp.refresh_token, - displayName: decodedToken.name, - buildingId: 4, - }; - localStorage.setItem("currentUser", JSON.stringify(userProfile)); - // window.location.reload(); - }); + console.log( + "Token expired but user is active. Refresh token not implemented." + ); + + this.updateUserProfile(this.currentUser.refresh_token).subscribe( + (resp) => { + const decodedToken = jwtDecode( + resp.access_token + ); + localStorage.setItem( + "account_info", + JSON.stringify(decodedToken) + ); + const userProfile = { + access_token: resp.access_token, + refresh_token: resp.refresh_token, + displayName: decodedToken.name, + buildingId: 4, + }; + localStorage.setItem( + "currentUser", + JSON.stringify(userProfile) + ); + window.location.reload(); + } + ); } }); } @@ -154,22 +178,24 @@ export class LoginService { startTrackingActivity(): void { this.ngZone.runOutsideAngular(() => { const activityEvents$ = merge( - fromEvent(window, 'mousemove'), - fromEvent(window, 'click'), - fromEvent(window, 'keypress'), - fromEvent(window, 'scroll') + fromEvent(window, "mousemove"), + fromEvent(window, "click"), + fromEvent(window, "keypress"), + fromEvent(window, "scroll") ); activityEvents$ .pipe( startWith(null), - switchMap(() => merge( - timer(0).pipe(mapTo(true)), - timer(5 * 60 * 1000).pipe(mapTo(false)) // 5 minutes of inactivity - )), + switchMap(() => + merge( + timer(0).pipe(mapTo(true)), + timer(5 * 60 * 1000).pipe(mapTo(false)) // 5 minutes of inactivity + ) + ), debounceTime(300) ) - .subscribe(active => { + .subscribe((active) => { this.ngZone.run(() => this.activitySubject.next(active)); }); }); diff --git a/src/app/content/hemat-app/user-profile/profil-information/profil-information.component.ts b/src/app/content/hemat-app/user-profile/profil-information/profil-information.component.ts index fa13165..1a56a0e 100644 --- a/src/app/content/hemat-app/user-profile/profil-information/profil-information.component.ts +++ b/src/app/content/hemat-app/user-profile/profil-information/profil-information.component.ts @@ -61,7 +61,8 @@ export class ProfilInformationComponent { ngOnInit(): void { this.storedData = JSON.parse(localStorage.getItem("account_info")); this.currentUser = JSON.parse(localStorage.getItem("currentUser")); - this.authService.checkTokenAndRedirect(); + this.authService.startTokenCheck(); + this.authService.startTrackingActivity(); this.profilInfo = this.formBuilder.group({ firstName: ["", Validators.required], diff --git a/src/app/content/hemat-app/user-profile/update-password/update-password.component.ts b/src/app/content/hemat-app/user-profile/update-password/update-password.component.ts index 31398bd..ec8ae0f 100644 --- a/src/app/content/hemat-app/user-profile/update-password/update-password.component.ts +++ b/src/app/content/hemat-app/user-profile/update-password/update-password.component.ts @@ -40,7 +40,8 @@ export class UpdatePasswordComponent { ngOnInit(): void { this.storedData = JSON.parse(localStorage.getItem("account_info")); - this.authService.checkTokenAndRedirect(); + this.authService.startTokenCheck(); + this.authService.startTrackingActivity(); this.profilInfo = this.formBuilder.group({ userid: this.storedData.sub, currentPass: [""],