137 lines
3.5 KiB
JavaScript
137 lines
3.5 KiB
JavaScript
import decode from "jwt-decode";
|
|
import config from "./config";
|
|
import logger from "./logit";
|
|
import store from "../store";
|
|
//import axios from 'axios';
|
|
//import auth0 from 'auth0-js';
|
|
//import Router from 'vue-router';
|
|
//import Auth0Lock from 'auth0-lock';
|
|
|
|
//https://stackoverflow.com/questions/15551652/javascript-enum-flag-check
|
|
const AuthorizationRoles = {
|
|
///<summary>No role set</summary>
|
|
NoRole: 0,
|
|
///<summary>BizAdminLimited</summary>
|
|
BizAdminLimited: 1,
|
|
///<summary>BizAdminFull</summary>
|
|
BizAdminFull: 2,
|
|
///<summary>DispatchLimited</summary>
|
|
DispatchLimited: 4,
|
|
///<summary>DispatchFull</summary>
|
|
DispatchFull: 8,
|
|
///<summary>InventoryLimited</summary>
|
|
InventoryLimited: 16,
|
|
///<summary>InventoryFull</summary>
|
|
InventoryFull: 32,
|
|
///<summary>AccountingFull</summary>
|
|
AccountingFull: 64, //No limited role, not sure if there is a need
|
|
///<summary>TechLimited</summary>
|
|
TechLimited: 128,
|
|
///<summary>TechFull</summary>
|
|
TechFull: 256,
|
|
///<summary>SubContractorLimited</summary>
|
|
SubContractorLimited: 512,
|
|
///<summary>SubContractorFull</summary>
|
|
SubContractorFull: 1024,
|
|
///<summary>ClientLimited</summary>
|
|
ClientLimited: 2048,
|
|
///<summary>ClientFull</summary>
|
|
ClientFull: 4096,
|
|
///<summary>OpsAdminLimited</summary>
|
|
OpsAdminLimited: 8192,
|
|
///<summary>OpsAdminFull</summary>
|
|
OpsAdminFull: 16384
|
|
}; //end AuthorizationRoles
|
|
|
|
const TOKEN_KEY = "apitoken";
|
|
const USER_ROLES = AuthorizationRoles.NoRole;
|
|
|
|
export function processLogin(response) {
|
|
//is token present?
|
|
if (!response || !response.data || !response.data.token) {
|
|
logger.log("auth::processLogin -> token empty");
|
|
return Promise.reject();
|
|
}
|
|
const token = decode(response.data.token);
|
|
|
|
if (!token || !token.iss) {
|
|
logger.log("auth::processLogin -> token empty");
|
|
return Promise.reject();
|
|
}
|
|
|
|
if (token.iss != "ayanova.com") {
|
|
logger.log("auth::processLogin -> token invalid (iss)", token.iss);
|
|
return Promise.reject();
|
|
}
|
|
|
|
//Token is valid, store it in session storage
|
|
setToken(response.data.token);
|
|
|
|
//Put app relevant items into vuex store so app can use them
|
|
store.commit("authenticated", {
|
|
authenticated: true,
|
|
userId: Number(token.id),
|
|
roles: token["ayanova/roles"]
|
|
});
|
|
|
|
logger.log("User " + token.id + " logged in");
|
|
return Promise.resolve(true);
|
|
}
|
|
|
|
// var router = new Router({
|
|
// mode: 'history',
|
|
// });
|
|
|
|
export function processLogout() {
|
|
logger.log("Logout");
|
|
store.commit("notAuthenticated");
|
|
clearToken();
|
|
//router.go('/');
|
|
}
|
|
|
|
export function getToken() {
|
|
return sessionStorage.getItem(TOKEN_KEY);
|
|
}
|
|
|
|
function clearToken() {
|
|
sessionStorage.removeItem(TOKEN_KEY);
|
|
}
|
|
|
|
// Get and store token in local storage
|
|
export function setToken(token) {
|
|
sessionStorage.setItem(TOKEN_KEY, token);
|
|
}
|
|
|
|
export function isLoggedIn() {
|
|
//const token = getToken();
|
|
return !!config.apiToken && !isTokenExpired(config.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();
|
|
}
|
|
|
|
//================ ROLES =================
|
|
//https://stackoverflow.com/questions/39359740/what-are-enum-flags-in-typescript
|
|
export function hasRole(role) {
|
|
return role === (USER_ROLES & role);
|
|
// if ((role & flags.ERROR) == flags.ERROR) {
|
|
// alert("ERROR IS SET");
|
|
// }
|
|
}
|
|
|
|
//TODO: Auth JWT needs to return roles as an int enum
|