Files
the-jailor/wwwroot/core/base/baseTokenManager.js

51 lines
1.2 KiB
JavaScript

export class BaseTokenManager {
/**
*
* @param usersToken {UsersToken}
*/
constructor(usersToken) {
this.usersToken = usersToken;
this.watchDelay = 3600*1000; //1 hour in milliseconds
}
/**
* Launches watch for all users
*/
startWatching() {
this.usersToken.getAll().forEach((user, index) => {
this.watchUser(index);
})
}
/**
* Watches token for a single user
*/
watchUser(userIndex){
setInterval(async () => {
let userData = await this.getUserData(userIndex);
if(this.mustBeRenewed(userData.expiresAt)){
await this.renew(userData);
}
}, this.watchDelay);
}
renew(){
throw new Error("Not implemented.");
}
mustBeRenewed(expiresAt) {
const expirationThreshold = 3600*1000; //1 hour in milliseconds
const timeBeforeExpiration = expiresAt - Date.now();
return timeBeforeExpiration <= expirationThreshold;
}
getUserData(index){
return this.usersToken.get(index);
}
getAllUserData(){
return this.usersToken.getAll();
}
}