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($cookies) { this.$cookies = $cookies; try { this.token = sessionStorage.getItem('vnToken'); if (!this.token) this.token = localStorage.getItem('vnToken'); } catch (e) {} if (!this.token) this.token = this.$cookies.get('vnToken'); } set(value, remember) { this.unset(); try { if (remember) localStorage.setItem('vnToken', value); else sessionStorage.setItem('vnToken', value); } catch (e) { let options = {}; if (location.protocol == 'https:') options.secure = true; if (remember) { let now = new Date().getTime(); options.expires = new Date(now + 7 * 86400000); } this.$cookies.put('vnToken', value, options); } this.token = value; } unset() { localStorage.removeItem('vnToken'); sessionStorage.removeItem('vnToken'); this.$cookies.remove('vnToken'); this.token = null; } } Token.$inject = ['$cookies']; ngModule.service('vnToken', Token);