From 9cd5d36a5ff3df96c314bcc62834e0183acebad8 Mon Sep 17 00:00:00 2001 From: Juan Carlos Ruvalcaba Date: Thu, 22 Apr 2021 09:29:46 -0700 Subject: [PATCH] -Refactor/change user authentication pt2 --- src/js/controllers/check.js | 14 ++++++------- src/js/routes/login.js | 12 ++++++----- src/js/stores/user.js | 27 ++++++++++++++++-------- src/pages/home/panel-left.f7.html | 14 +++++++++---- src/pages/home/panel-right.f7.html | 30 +++++++++++++++++++-------- src/pages/login.f7.html | 33 +++++++++++++++++++++++------- 6 files changed, 89 insertions(+), 41 deletions(-) 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 @@ -
  • @@ -43,6 +42,7 @@
  • + ${isSeller && $h`
  • @@ -61,6 +61,8 @@
  • + `} + ${isDeliverer && $h`
  • @@ -78,7 +80,9 @@
  • -
  • + `} + ${isCustomer && $h` +
  • Clientes
    @@ -97,6 +101,7 @@
  • + `}
  • @@ -135,8 +140,9 @@ export default (props, { $f7, $on, $update, }) => { const onPanelOpen = () => { if (User.isLogged()){ - const userData = JSON.parse(localStorage.getItem("userData")); - const userRoles = userData['userPermissions']; + //const userData = JSON.parse(localStorage.getItem("userData")); + //const userRoles = userData['userPermissions']; + const userRoles = user.getters.roles.value; isLogged = true, isCustomer = userRoles.includes(4), //Customer, diff --git a/src/pages/home/panel-right.f7.html b/src/pages/home/panel-right.f7.html index dd82e10..9776bba 100644 --- a/src/pages/home/panel-right.f7.html +++ b/src/pages/home/panel-right.f7.html @@ -63,24 +63,36 @@