import store from '@/store';

// ============Auth Start===================
const AuthKey = 'MTA-Auth';

export function getAuth() {
    if (!store.state.auth) {
        let auth = JSON.parse(window.localStorage.getItem(AuthKey));
        store.commit({
                         type:   'setAuth'
                         , auth: auth,
                     });
        return auth;
    } else {
        return store.state.auth;
    }
}

export function setAuth(auth) {
    if (auth === undefined) {
        window.localStorage.removeItem(AuthKey);
        store.state.auth = null;
    } else {
        window.localStorage.setItem(AuthKey, JSON.stringify(auth));
        store.state.auth = auth;
    }
}

export function removeAuth() {
    window.localStorage.removeItem(AuthKey);
    // delete store.state.auth;
    store.commit({
                     type:   'setAuth'
                     , auth: null,
                 });
}
// ============Auth End===================
