Mercurial > gemma
view client/src/stores/user.js @ 514:4a1db55a9920
Use auth.RunAs in JSON controller.
It's more symmetric to the rest of the application and
returns the database connection earlier to the pool.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 27 Aug 2018 10:35:55 +0200 |
parents | 2ac37419f593 |
children | 04a6bea229e4 |
line wrap: on
line source
import { HTTP } from "../lib/http"; const User = { namespaced: true, state: { authenticated: false, expires: null, roles: [], user: "" }, getters: { isAuthenticated: state => { return state.authenticated; }, userinfo: state => { return state.user; }, roles: state => { return state.roles; }, expires: state => { return state.expires; }, isWaterwayAdmin: state => { return state.roles.includes("waterway_admin"); }, isSysAdmin: state => { return state.roles.includes("sys_admin"); } }, mutations: { auth_success: (state, data) => { const { token, user, expires, roles } = data; localStorage.setItem("expires", expires); localStorage.setItem("roles", roles); localStorage.setItem("token", token); localStorage.setItem("user", user); state.expires = expires; state.roles = roles; state.user = user; state.authenticated = true; }, clear_auth: state => { state.authenticated = false; state.expires = null; state.roles = []; state.user = ""; localStorage.clear(); }, set_user: (state, name) => { state.user = name; }, set_roles: (state, roles) => { state.roles = roles; }, set_expires: (state, expires) => { state.expires = expires; }, set_authenticate: state => { state.authenticated = true; } }, actions: { login({ commit }, user) { // using POST is a bit more secure than GET return new Promise((resolve, reject) => { // axios will add the application/x-www-form-urlencoded header this way HTTP.post("/login", user) .then(response => { commit("auth_success", response.data); resolve(response); }) .catch(error => { commit("clear_auth"); reject(error); }); }); } } }; export default User;