2019-01-23 12:11:44 +00:00
|
|
|
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 {
|
2019-01-25 11:04:35 +00:00
|
|
|
constructor() {
|
2019-01-23 12:11:44 +00:00
|
|
|
try {
|
|
|
|
this.token = sessionStorage.getItem('vnToken');
|
2023-06-07 08:28:29 +00:00
|
|
|
this.created = sessionStorage.getItem('vnTokenCreated');
|
2023-06-27 13:34:35 +00:00
|
|
|
this.renewPeriod = sessionStorage.getItem('vnTokenRenewPeriod');
|
2023-06-07 08:28:29 +00:00
|
|
|
if (!this.token) {
|
2019-01-23 12:11:44 +00:00
|
|
|
this.token = localStorage.getItem('vnToken');
|
2023-06-07 08:28:29 +00:00
|
|
|
this.created = localStorage.getItem('vnTokenCreated');
|
2023-06-27 13:34:35 +00:00
|
|
|
this.renewPeriod = localStorage.getItem('vnTokenRenewPeriod');
|
2023-06-07 08:28:29 +00:00
|
|
|
}
|
2019-01-23 12:11:44 +00:00
|
|
|
} catch (e) {}
|
|
|
|
}
|
2023-06-27 13:34:35 +00:00
|
|
|
set(token, created, renewPeriod, remember) {
|
2019-01-23 12:11:44 +00:00
|
|
|
this.unset();
|
|
|
|
try {
|
2023-06-07 08:28:29 +00:00
|
|
|
if (remember) {
|
|
|
|
localStorage.setItem('vnToken', token);
|
|
|
|
localStorage.setItem('vnTokenCreated', created);
|
2023-06-27 13:34:35 +00:00
|
|
|
localStorage.setItem('vnTokenRenewPeriod', renewPeriod);
|
2023-06-07 08:28:29 +00:00
|
|
|
} else {
|
|
|
|
sessionStorage.setItem('vnToken', token);
|
|
|
|
sessionStorage.setItem('vnTokenCreated', created);
|
2023-06-27 13:34:35 +00:00
|
|
|
sessionStorage.setItem('vnTokenRenewPeriod', renewPeriod);
|
2023-06-07 08:28:29 +00:00
|
|
|
}
|
2019-01-25 11:04:35 +00:00
|
|
|
} catch (e) {}
|
2019-01-23 12:11:44 +00:00
|
|
|
|
2023-06-07 08:28:29 +00:00
|
|
|
this.token = token;
|
|
|
|
this.created = created;
|
2023-06-27 13:34:35 +00:00
|
|
|
this.renewPeriod = renewPeriod;
|
2019-01-23 12:11:44 +00:00
|
|
|
}
|
|
|
|
unset() {
|
|
|
|
localStorage.removeItem('vnToken');
|
|
|
|
sessionStorage.removeItem('vnToken');
|
|
|
|
this.token = null;
|
2023-06-07 08:28:29 +00:00
|
|
|
this.created = null;
|
2023-06-27 13:34:35 +00:00
|
|
|
this.renewPeriod = null;
|
2019-01-23 12:11:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ngModule.service('vnToken', Token);
|