131 lines
3.8 KiB
JavaScript
131 lines
3.8 KiB
JavaScript
/* xeslint-disable */
|
|
//import decode from "jwt-decode";
|
|
import jwt_decode from "jwt-decode";
|
|
import initialize from "./initialize";
|
|
import notifypoll from "./notifypoll";
|
|
|
|
export function processLogin(authResponse, loggedInWithKnownPassword) {
|
|
return new Promise(async function(resolve, reject) {
|
|
try {
|
|
//check there is a response of some kind
|
|
if (!authResponse) {
|
|
window.$gz.store.commit("logItem", "auth::processLogin -> no response");
|
|
return reject();
|
|
}
|
|
|
|
//is token present?
|
|
if (!authResponse || !authResponse.token) {
|
|
window.$gz.store.commit(
|
|
"logItem",
|
|
"auth::processLogin -> response contains no data"
|
|
);
|
|
return reject();
|
|
}
|
|
const token = jwt_decode(authResponse.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
|
|
|
|
//encourage password changing if a purchased license
|
|
if (loggedInWithKnownPassword)
|
|
window.$gz.store.commit("setKnownPassword", true);
|
|
|
|
//Put app relevant items into vuex store so app can use them
|
|
window.$gz.store.commit("login", {
|
|
apiToken: authResponse.token,
|
|
authenticated: true,
|
|
userId: Number(token.id),
|
|
userName: authResponse.name,
|
|
roles: authResponse.roles,
|
|
userType: authResponse.usertype,
|
|
dlt: authResponse.dlt,
|
|
tfaEnabled: authResponse.tfa
|
|
});
|
|
//log the login
|
|
window.$gz.store.commit(
|
|
"logItem",
|
|
"auth::processLogin -> User " + token.id + " logged in"
|
|
);
|
|
|
|
//Get global settings
|
|
let gsets = await window.$gz.api.get("global-biz-setting/client");
|
|
if (gsets.error) {
|
|
//In a form this would trigger a bunch of validation or error display code but for here and now:
|
|
//convert error to human readable string for display and popup a notification to user
|
|
let msg = window.$gz.api.apiErrorToHumanString(gsets.error);
|
|
window.$gz.store.commit(
|
|
"logItem",
|
|
"Initialize::() fetch global-biz-setting/client -> error" + msg
|
|
);
|
|
window.$gz.eventBus.$emit("notify-error", msg);
|
|
} else {
|
|
//Check if overrides and use them here
|
|
//or else use browser defaults
|
|
window.$gz.store.commit("setGlobalSettings", gsets.data);
|
|
}
|
|
await initialize();
|
|
} catch (err) {
|
|
reject(err);
|
|
}
|
|
|
|
//start notification polling
|
|
notifypoll.startPolling();
|
|
resolve();
|
|
//-------------------------------------------------
|
|
});
|
|
}
|
|
|
|
export function processLogout() {
|
|
notifypoll.stopPolling();
|
|
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 = jwt_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();
|
|
}
|