Initial commit
This commit is contained in:
47
wwwroot/core/base/baseTokenManager.js
Normal file
47
wwwroot/core/base/baseTokenManager.js
Normal file
@@ -0,0 +1,47 @@
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user