From 44b823e3766e003adf803d22c7b1d6418520de41 Mon Sep 17 00:00:00 2001 From: joan Date: Wed, 2 Mar 2022 15:29:41 +0100 Subject: [PATCH] Keep session and added Topbar component --- src/assets/logo_icon.svg | 22 +++++ src/components/Topbar.vue | 146 +++++++++++++++++++++++++++++++++ src/components/UserPanel.vue | 0 src/locales/es.json | 4 +- src/router/index.ts | 15 ++-- src/store/index.ts | 44 ++++++++-- src/views/Customer/Main.vue | 16 ++++ src/views/Login/Login.vue | 39 +++++---- src/views/Main/Dashboard.vue | 6 +- src/views/Main/Main.vue | 155 +++-------------------------------- 10 files changed, 270 insertions(+), 177 deletions(-) create mode 100644 src/assets/logo_icon.svg create mode 100644 src/components/Topbar.vue create mode 100644 src/components/UserPanel.vue create mode 100644 src/views/Customer/Main.vue diff --git a/src/assets/logo_icon.svg b/src/assets/logo_icon.svg new file mode 100644 index 000000000..b6fdcca4e --- /dev/null +++ b/src/assets/logo_icon.svg @@ -0,0 +1,22 @@ + + + + + + + + + image/svg+xml + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/components/Topbar.vue b/src/components/Topbar.vue new file mode 100644 index 000000000..1830ddbc6 --- /dev/null +++ b/src/components/Topbar.vue @@ -0,0 +1,146 @@ + + + diff --git a/src/components/UserPanel.vue b/src/components/UserPanel.vue new file mode 100644 index 000000000..e69de29bb diff --git a/src/locales/es.json b/src/locales/es.json index a317041a2..2b938e10e 100644 --- a/src/locales/es.json +++ b/src/locales/es.json @@ -3,7 +3,9 @@ "lang": { "es": "Español", "en": "Inglés" - } + }, + "accessDenied": "Acceso denegado", + "internalServerError": "Ha ocurrido un error interno del servidor" }, "login": { "title": "Iniciar sesión", diff --git a/src/router/index.ts b/src/router/index.ts index d69e4c7e4..a1fffe7de 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -1,4 +1,5 @@ import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'; +import store from '../store'; const routes: Array = [ { @@ -12,15 +13,15 @@ const routes: Array = [ component: () => import('../views/Main/Main.vue'), children: [ { - path: '', + path: '/dashboard', name: 'Dashboard', component: () => import('../views/Main/Dashboard.vue'), }, - /* { - path: '/Client', - name: 'Client', - component: () => import('../views/Client/client.vue'), - }, */ + { + path: '/customer', + name: 'Customer', + component: () => import('../views/Customer/Main.vue'), + }, { path: '/:pathMatch(.*)*', name: 'NotFound', @@ -36,7 +37,7 @@ const router = createRouter({ }); router.beforeEach((to, from, next) => { - const loggedIn = localStorage.getItem('token'); + const loggedIn = store.getters.isLoggedIn; if (to.name !== 'Login' && !loggedIn) { next({ path: '/login', query: { redirect: to.fullPath } }); diff --git a/src/store/index.ts b/src/store/index.ts index fda650d0d..e6a8d9351 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -1,12 +1,26 @@ import { createStore } from 'vuex'; +interface UserProfile { + id: number; + username: string; + nickname: string; + token: string; +} + export default createStore({ state: { - user: null, + user: {}, roles: [], }, mutations: { - setUser(state, user) { + setUser(state, data) { + const user: UserProfile = { + id: data.id, + username: data.name, + nickname: data.nickname, + token: data.token, + }; + state.user = user; }, setRoles(state, roles) { @@ -14,15 +28,20 @@ export default createStore({ }, }, actions: { - logIn({ commit }, auth) { - localStorage.setItem('token', auth.token); + logIn({ commit }, data) { + if (data.keepLogin) { + localStorage.setItem('token', data.token); + } else { + sessionStorage.setItem('token', data.token); + } - commit('setUser', auth); + commit('setUser', data); }, logOut({ commit }) { localStorage.removeItem('token'); + sessionStorage.getItem('token'); - commit('setUser', null); + commit('setUser', {}); }, updateUserData({ commit }, data) { commit('setUser', data.user); @@ -30,8 +49,17 @@ export default createStore({ }, }, getters: { - hasData(state) { - return !!state.user; + isLoggedIn() { + const localToken = localStorage.getItem('token'); + const sessionToken = sessionStorage.getItem('token'); + + return !!(localToken || sessionToken); + }, + token() { + const localToken = localStorage.getItem('token'); + const sessionToken = sessionStorage.getItem('token'); + + return localToken || sessionToken; }, }, modules: {}, diff --git a/src/views/Customer/Main.vue b/src/views/Customer/Main.vue new file mode 100644 index 000000000..1abf919af --- /dev/null +++ b/src/views/Customer/Main.vue @@ -0,0 +1,16 @@ + + + + + diff --git a/src/views/Login/Login.vue b/src/views/Login/Login.vue index 50fa2d709..547c95b1a 100644 --- a/src/views/Login/Login.vue +++ b/src/views/Login/Login.vue @@ -55,7 +55,7 @@