diff --git a/src/js/controllers/check.js b/src/js/controllers/check.js index bab5666..2591fc0 100644 --- a/src/js/controllers/check.js +++ b/src/js/controllers/check.js @@ -1,11 +1,11 @@ -import User from './users.js'; -//import user from './../stores/user.js'; +//import User from './users.js'; +import user from './../stores/user.js'; let Check = { authorization( { to, resolve } ) { const router = this; - //if (user.isLogged.value){ - if (!User.isLogged()){ + if (user.getters.isLogged.value){ + //if (!User.isLogged()){ router.navigate('/login/', { reloadCurrent: true }); } else{ @@ -14,9 +14,9 @@ let Check = { }, permission( { to, resolve, reject } ) { const router = this; - const userData = JSON.parse(localStorage.getItem("userData")); - const userRoles = userData['userPermissions']; - //const userRoles = user.roles.value; + //const userData = JSON.parse(localStorage.getItem("userData")); + //const userRoles = userData['userPermissions']; + const userRoles = user.getters.roles.value; let currentIndex = -1; for (let i = 0; i < router.routes.length; i++) { diff --git a/src/js/routes/login.js b/src/js/routes/login.js index 611febd..42d49b9 100644 --- a/src/js/routes/login.js +++ b/src/js/routes/login.js @@ -1,16 +1,18 @@ import LoginPage from '../../pages/login.f7.html'; -import User from '../controllers/users.js'; +//import User from '../controllers/users.js'; +import user from '../stores/user.js'; function checkAuth( { resolve } ) { const router = this; - if (User.isLogged()){ + if (user.getters.isLogged.value){ + //if (User.isLogged()){ router.navigate('/', { reloadCurrent: true }); } else { - if (router.currentRoute.name !== "login"){ - resolve('/login/'); - } + if (router.currentRoute.name !== "login"){ + resolve('/login/'); + } } } diff --git a/src/js/stores/user.js b/src/js/stores/user.js index 56bf751..1b86612 100644 --- a/src/js/stores/user.js +++ b/src/js/stores/user.js @@ -3,7 +3,7 @@ import http from './../api/config.js'; const user = createStore({ state: { - status: '', + status: localStorage.getItem('token') ? 'success' : '', token: localStorage.getItem('token') || '', user: {}, roles: [], @@ -12,11 +12,13 @@ const user = createStore({ logIn({ state, dispatch }, data){ return new Promise((resolve, reject) => { dispatch('auth_request'); - http.post('/user/login', data) - .then(response => { + http.post('/user/login', data).then(response => { const token = response.data.token; - const user = response.data.user; - const roles = response.data.roles; + delete response.data.token; + const user = response.data; + const roles = [1, 4]; //Owner, Customer, Seller, Deliverer//response.data.roles; + console.log(roles); + console.log(user); localStorage.setItem('token', token); http.defaults.headers.common['Authorization'] = token; dispatch('auth_success', token, user, roles); @@ -29,9 +31,10 @@ const user = createStore({ }); }); }, - logOut(){ + logOut({ state, dispatch }){ return new Promise((resolve, reject) => { dispatch('logout'); + localStorage.removeItem('token') http.defaults.headers.common['Authorization']; resolve(); }); @@ -41,16 +44,16 @@ const user = createStore({ state.status = ''; state.token = ''; }, - auth_request(state){ + auth_request({ state }){ state.status = 'loading'; }, - auth_success(state, token, user, roles){ + auth_success({ state }, token, user, roles){ state.status = 'success'; state.token = token; state.user = user; state.roles = roles; }, - auth_error(state){ + auth_error({ state }){ state.status = 'error' }, //End mutations @@ -59,12 +62,18 @@ const user = createStore({ isLogged({ state }) { return !!state.token; }, + token({ state }) { + return state.token; + }, authStatus({ state }) { return state.status; }, roles({ state }){ return state.roles; }, + displayName({ state }){ + return state.user; + } }, }) diff --git a/src/pages/home/panel-left.f7.html b/src/pages/home/panel-left.f7.html index 49c443f..72e2835 100644 --- a/src/pages/home/panel-left.f7.html +++ b/src/pages/home/panel-left.f7.html @@ -27,7 +27,6 @@ -