salix/front/core/services/token.js

49 lines
1.6 KiB
JavaScript

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');
this.renewPeriod = sessionStorage.getItem('vnTokenRenewPeriod');
if (!this.token) {
this.token = localStorage.getItem('vnToken');
this.created = localStorage.getItem('vnTokenCreated');
this.renewPeriod = localStorage.getItem('vnTokenRenewPeriod');
}
} catch (e) {}
}
set(token, created, renewPeriod, remember) {
this.unset();
try {
if (remember) {
localStorage.setItem('vnToken', token);
localStorage.setItem('vnTokenCreated', created);
localStorage.setItem('vnTokenRenewPeriod', renewPeriod);
} else {
sessionStorage.setItem('vnToken', token);
sessionStorage.setItem('vnTokenCreated', created);
sessionStorage.setItem('vnTokenRenewPeriod', renewPeriod);
}
} catch (e) {}
this.token = token;
this.created = created;
this.renewPeriod = renewPeriod;
}
unset() {
localStorage.removeItem('vnToken');
sessionStorage.removeItem('vnToken');
this.token = null;
this.created = null;
this.renewPeriod = null;
}
}
ngModule.service('vnToken', Token);