105 lines
2.6 KiB
JavaScript
105 lines
2.6 KiB
JavaScript
/* xeslint-disable */
|
|
import decode from "jwt-decode";
|
|
import initialize from "./initialize";
|
|
|
|
export function processLogin(response) {
|
|
return new Promise(function(resolve, reject) {
|
|
//check there is a response of some kind
|
|
if (!response) {
|
|
window.$gz.store.commit("logItem", "auth::processLogin -> no response");
|
|
return reject();
|
|
}
|
|
|
|
//is there an error?
|
|
if (response.error) {
|
|
return reject(response.error);
|
|
}
|
|
|
|
//is token present?
|
|
if (!response.data || !response.data.token) {
|
|
window.$gz.store.commit(
|
|
"logItem",
|
|
"auth::processLogin -> response contains no data"
|
|
);
|
|
return reject();
|
|
}
|
|
const token = decode(response.data.token);
|
|
|
|
if (!token || !token.iss) {
|
|
window.$gz.store.commit(
|
|
"logItem",
|
|
"auth::processLogin -> response token empty"
|
|
);
|
|
return reject();
|
|
}
|
|
|
|
if (token.iss != "ayanova.com") {
|
|
window.$gz.store.commit(
|
|
"logItem",
|
|
"auth::processLogin -> token invalid (iss): " + token.iss
|
|
);
|
|
return reject();
|
|
}
|
|
|
|
//ensure the store is clean first in case we didn't come here from a clean logout
|
|
window.$gz.store.commit("logout");
|
|
sessionStorage.clear(); //clear all temporary session storage data
|
|
|
|
//Put app relevant items into vuex store so app can use them
|
|
window.$gz.store.commit("login", {
|
|
apiToken: response.data.token,
|
|
authenticated: true,
|
|
userId: Number(token.id),
|
|
userName: response.data.name,
|
|
roles: response.data.roles,
|
|
userType: response.data.usertype,
|
|
dlt: response.data.dlt
|
|
});
|
|
|
|
//Initialize the application
|
|
initialize().then(() => {
|
|
window.$gz.store.commit(
|
|
"logItem",
|
|
"auth::processLogin -> User " + token.id + " logged in"
|
|
);
|
|
resolve(true);
|
|
});
|
|
});
|
|
}
|
|
|
|
export function processLogout() {
|
|
if (window.$gz.store.state.authenticated) {
|
|
window.$gz.store.commit(
|
|
"logItem",
|
|
"auth::processLogout -> User logged out"
|
|
);
|
|
}
|
|
window.$gz.store.commit("logout");
|
|
sessionStorage.clear(); //clear all temporary session storage data
|
|
}
|
|
|
|
export function isLoggedIn() {
|
|
//const token = getToken();
|
|
return (
|
|
!!window.$gz.store.state.apiToken &&
|
|
!isTokenExpired(window.$gz.store.state.apiToken)
|
|
);
|
|
}
|
|
|
|
function getTokenExpirationDate(encodedToken) {
|
|
const token = decode(encodedToken);
|
|
if (!token.exp) {
|
|
return null;
|
|
}
|
|
|
|
const date = new Date(0);
|
|
date.setUTCSeconds(token.exp);
|
|
|
|
return date;
|
|
}
|
|
|
|
function isTokenExpired(token) {
|
|
const expirationDate = getTokenExpirationDate(token);
|
|
return expirationDate < new Date();
|
|
}
|