salix/front/core/services/token.js

49 lines
1.6 KiB
JavaScript
Raw Normal View History

import ngModule from '../module';
/**
* Saves and loads the token for the current logged in user.
*
* @property {String} token The current login token or %null
*/
export default class Token {
constructor() {
try {
this.token = sessionStorage.getItem('vnToken');
this.created = sessionStorage.getItem('vnTokenCreated');
2023-06-27 13:34:35 +00:00
this.renewPeriod = sessionStorage.getItem('vnTokenRenewPeriod');
if (!this.token) {
this.token = localStorage.getItem('vnToken');
this.created = localStorage.getItem('vnTokenCreated');
2023-06-27 13:34:35 +00:00
this.renewPeriod = localStorage.getItem('vnTokenRenewPeriod');
}
} catch (e) {}
}
2023-06-27 13:34:35 +00:00
set(token, created, renewPeriod, remember) {
this.unset();
try {
if (remember) {
localStorage.setItem('vnToken', token);
localStorage.setItem('vnTokenCreated', created);
2023-06-27 13:34:35 +00:00
localStorage.setItem('vnTokenRenewPeriod', renewPeriod);
} else {
sessionStorage.setItem('vnToken', token);
sessionStorage.setItem('vnTokenCreated', created);
2023-06-27 13:34:35 +00:00
sessionStorage.setItem('vnTokenRenewPeriod', renewPeriod);
}
} catch (e) {}
this.token = token;
this.created = created;
2023-06-27 13:34:35 +00:00
this.renewPeriod = renewPeriod;
}
unset() {
localStorage.removeItem('vnToken');
sessionStorage.removeItem('vnToken');
this.token = null;
this.created = null;
2023-06-27 13:34:35 +00:00
this.renewPeriod = null;
}
}
ngModule.service('vnToken', Token);