Skip to content

Instantly share code, notes, and snippets.

@robneville73
Last active January 27, 2018 23:55
Show Gist options
  • Save robneville73/324c5ff86fd5ac3281f10f2477f14e54 to your computer and use it in GitHub Desktop.
Save robneville73/324c5ff86fd5ac3281f10f2477f14e54 to your computer and use it in GitHub Desktop.
global guard not working
import axios from "axios";
import router from "../../router";
const state = {
authenticated: false,
auth: {},
loggedin_siteid: null,
login_inprogress: false
};
const getters = {
authenticated: state => {
return state.authenticated;
}
};
const mutations = {
initializeAuth(state, auth_obj) {
state.auth = auth_obj;
state.authenticated = true;
router.push({ name: "main" });
},
updateLoggedInSite(state, siteid) {
state.siteid = siteid;
},
loginStart(state) {
state.login_inprogress = true;
},
loginFinish(state) {
state.login_inprogress = false;
}
};
const actions = {
authenticate({ commit }, login_obj) {
commit("loginStart");
axios
.get("/authenticate", {
params: {
username: login_obj.username,
password: login_obj.password,
siteid: login_obj.siteid,
flex: "true"
}
})
.then(({ data: { response: response } }) => {
commit("loginFinish");
const errors = response.errors;
if (errors) {
console.log(errors);
} else {
commit("initializeAuth", response.results[0]);
commit("updateLoggedInSite", login_obj.siteid);
}
});
}
};
export default {
state,
mutations,
actions,
getters
};
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from "vue";
import Vuetify from "vuetify";
import axios from "axios";
import App from "./App";
import store from "./store/store";
import router from "./router";
import("vuetify/dist/vuetify.min.css"); // Ensure you are using css-loader
axios.defaults.baseURL = "https://jayne.retailarchitects.com/tg/";
Vue.config.productionTip = false;
Vue.use(Vuetify);
/* eslint-disable no-new */
new Vue({
el: "#app",
router,
store,
components: { App },
template: "<App/>"
});
import Vue from "vue";
import Router from "vue-router";
import store from "../store/store";
import Login from "@/components/Login";
import Main from "@/components/Main";
import Location from "@/components/Location";
Vue.use(Router);
const router = new Router({
routes: [
{
path: "/",
name: "main",
component: Main
},
{
path: "/login",
name: "login",
component: Login
},
{
path: "/location/:locationid",
name: "location",
component: Location
}
]
});
router.beforeEach((to, from, next) => {
if (to.name !== "login" && !store.getters.authenticated) {
next({ name: "login" });
} else {
next();
}
});
export default router;
import Vue from "vue";
import Vuex from "vuex";
import authenticate from "./modules/authenticate";
Vue.use(Vuex);
const store = new Vuex.Store({
modules: [authenticate]
});
export default store;
@robneville73
Copy link
Author

the store import works fine elsewhere in that I'm able to do this.$store inside of components just fine. When I hit the debugger statement store isn't defined?? I've searched a bit and found several SO solutions that say to do what I'm doing here (or at least as far as I can tell).

@robneville73
Copy link
Author

also tried moving the guard clause after the root Vue object is instantiated just for giggles...no good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment