This commit is contained in:
2018-11-08 18:05:00 +00:00
parent 8c7a001bca
commit 550b40edcc
3 changed files with 38 additions and 50 deletions

View File

@@ -6,11 +6,22 @@ Vue.use(Vuex);
export default new Vuex.Store({
state: {
authenticated: false,
mockAccount: {
username: "manager",
password: "letmein"
apiUrl: "http://localhost:7575/api/v8.0/",
userId: 0,
roles: 0
},
mutations: {
authenticated(state, data) {
// mutate state
state.authenticated = data.authenticated;
state.userId = data.userId;
state.roles = data.roles;
},
notAuthenticated(state) {
state.authenticated = false;
state.userId = 0;
state.roles = 0;
}
},
mutations: {},
actions: {}
});

View File

@@ -1,6 +1,7 @@
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';
@@ -43,26 +44,9 @@ const AuthorizationRoles = {
}; //end AuthorizationRoles
const TOKEN_KEY = "apitoken";
const USER_ROLES = AuthorizationRoles.NoRole;
// const CLIENT_ID = '{AUTH0_CLIENT_ID}';
// const CLIENT_DOMAIN = '{AUTH0_DOMAIN}';
// const REDIRECT = 'YOUR_CALLBACK_URL';
// const SCOPE = '{SCOPE}';
// const AUDIENCE = 'AUDIENCE_ATTRIBUTE';
// var auth = new auth0.WebAuth({
// clientID: CLIENT_ID,
// domain: CLIENT_DOMAIN
// });
export function processLogin(response) {
//validate token (ensure it's *our* token at least, the server will do the real validation on requests)
//response.data.token
//store token in central store
//todo: put token into localstorage later once this validation is worked out
//is token present?
if (!response || !response.data || !response.data.token) {
logger.log("auth::processLogin -> token empty");
@@ -80,9 +64,15 @@ export function processLogin(response) {
return Promise.reject();
}
config.apiToken = response.data.token;
config.userId = Number(token.id);
config.roles = token["ayanova/roles"];
//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);
@@ -94,38 +84,22 @@ export function processLogin(response) {
export function processLogout() {
logger.log("Logout");
store.commit("notAuthenticated");
clearToken();
//router.go('/');
}
// export function requireAuth(to, from, next) {
// if (!isLoggedIn()) {
// next({
// path: '/',
// query: { redirect: to.fullPath }
// });
// } else {
// next();
// }
// }
export function getIdToken() {
return localStorage.getItem(TOKEN_KEY);
export function getToken() {
return sessionStorage.getItem(TOKEN_KEY);
}
function clearToken() {
localStorage.removeItem(TOKEN_KEY);
sessionStorage.removeItem(TOKEN_KEY);
}
// // Helper function that will allow us to extract the access_token and id_token
// function getParameterByName(name) {
// let match = RegExp("[#&]" + name + "=([^&]*)").exec(window.location.hash);
// return match && decodeURIComponent(match[1].replace(/\+/g, " "));
// }
// Get and store id_token in local storage
// Get and store token in local storage
export function setToken(token) {
localStorage.setItem(TOKEN_KEY, token);
sessionStorage.setItem(TOKEN_KEY, token);
}
export function isLoggedIn() {

View File

@@ -88,10 +88,13 @@ VUEX STATE PERSISTENCE = VUEX-PERSISTEDSTATE
- If recreating AyaNova 7 essentially, then that app is always online as well so maybe don't even consider offline work?
LOCALSTORAGE = STORE.JS
=-=-=-=-=-=-=-=-=-=-
- Very widely used and I don't necessarily need a VUE specific one, this is the one used for pecklist and rockfish
- https://github.com/marcuswestin/store.js/
LOCALSTORAGE = Built in
=-=-=-=-=-=-=-=-=-=-=-=
- All modern browsers should support it and less libs the better
- Fallback if find it doesn't work:
- = STORE.JS Very widely used and I don't necessarily need a VUE specific one, this is the one used for pecklist and rockfish
- https://github.com/marcuswestin/store.js/
SERVICEWORKER = WORKBOX
=-=-=-=-=-=-=-=-=-=-=-=