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');
            if (!this.token)
                this.token = localStorage.getItem('vnToken');
        } catch (e) {}
    }
    set(value, remember) {
        this.unset();
        try {
            if (remember)
                localStorage.setItem('vnToken', value);
            else
                sessionStorage.setItem('vnToken', value);
        } catch (e) {}

        this.token = value;
    }
    unset() {
        localStorage.removeItem('vnToken');
        sessionStorage.removeItem('vnToken');
        this.token = null;
    }
}

ngModule.service('vnToken', Token);