implement refresh token
This commit is contained in:
@@ -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<CustomJwtPayload>(tokenInfo.refresh_token);
|
||||
const decodedToken = jwtDecode<CustomJwtPayload>(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<CustomJwtPayload>(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<CustomJwtPayload>(
|
||||
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));
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user