From 82d4b52e24f6abc80c9a4abf917c71e35de85705 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Thu, 15 Oct 2020 03:36:25 +0200 Subject: [PATCH] Cleanup + Login with plugin backend --- src/boot/login.ts | 29 ++++++++------ src/pages/Login.vue | 39 ++++++++++++------- src/plugins/plugin-example/routes/index.ts | 3 ++ src/plugins/user/pages/User.vue | 33 +++++++++++----- src/router/routes.ts | 3 +- src/store/index.ts | 12 +++--- src/store/module-example/actions.ts | 11 ------ src/store/module-example/mutations.ts | 10 ----- src/store/module-example/state.ts | 9 ----- src/store/module-user/actions.ts | 25 ++++++++++++ .../getters.ts | 5 ++- .../{module-example => module-user}/index.ts | 4 +- src/store/module-user/mutations.ts | 16 ++++++++ src/store/module-user/state.ts | 29 ++++++++++++++ 14 files changed, 152 insertions(+), 76 deletions(-) delete mode 100644 src/store/module-example/actions.ts delete mode 100644 src/store/module-example/mutations.ts delete mode 100644 src/store/module-example/state.ts create mode 100644 src/store/module-user/actions.ts rename src/store/{module-example => module-user}/getters.ts (50%) rename src/store/{module-example => module-user}/index.ts (68%) create mode 100644 src/store/module-user/mutations.ts create mode 100644 src/store/module-user/state.ts diff --git a/src/boot/login.ts b/src/boot/login.ts index bcc9d7a..3373626 100644 --- a/src/boot/login.ts +++ b/src/boot/login.ts @@ -1,20 +1,25 @@ import { boot } from 'quasar/wrappers'; +import { StateInterface } from '../store'; export default boot(({ Vue, router, store }) => { router.beforeEach((to, from, next) => { - if (to.matched.some(record => record.meta.requiresAuth)) { - // this route requires auth, check if logged in - // if not, redirect to login page. - if (!store.getters.isLoggedIn()) { - next({ - path: '/login', - query: { redirect: to.fullPath } - }) - } else { - next() - } + let user = (store.state).user; + + if (to.matched.some(record => { + // permissions is set AND has NO matching permission + return record.meta.permissions !== undefined && + !(record.meta.permissions.filter( + (value: string) => + user.permissions.includes(value) + ).length > 0); + }) + ) { + next({ + path: '/login', + query: { redirect: to.fullPath } + }); } else { - next() // make sure to always call next()! + next(); } }) }); diff --git a/src/pages/Login.vue b/src/pages/Login.vue index 66e3199..b36f69f 100644 --- a/src/pages/Login.vue +++ b/src/pages/Login.vue @@ -1,8 +1,9 @@ diff --git a/src/router/routes.ts b/src/router/routes.ts index ca5c399..9ff5656 100644 --- a/src/router/routes.ts +++ b/src/router/routes.ts @@ -20,12 +20,13 @@ const routes: RouteConfig[] = [ }, { path: '/main', - name: 'main', + redirect: 'user', component: () => import('layouts/MainLayout.vue'), children: [ { name: 'about', path: 'about', + meta: { 'permissions': ['user'] }, component: () => import('pages/about/About.vue') } ] diff --git a/src/store/index.ts b/src/store/index.ts index 76aafdb..9b6b8fb 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -1,8 +1,8 @@ import { store } from 'quasar/wrappers'; import Vuex from 'vuex'; -// import example from './module-example'; -// import { ExampleStateInterface } from './module-example/state'; +import user from './module-user'; +import { UserStateInterface } from './module-user/state'; /* * If not building with SSR mode, you can @@ -11,17 +11,15 @@ import Vuex from 'vuex'; export interface StateInterface { // Define your own store structure, using submodules if needed - // example: ExampleStateInterface; - // Declared as unknown to avoid linting issue. Best to strongly type as per the line above. - example: unknown; + user: UserStateInterface; } -export default store(function({ Vue }) { +export default store(function ({ Vue }) { Vue.use(Vuex); const Store = new Vuex.Store({ modules: { - // example + user }, // enable strict mode (adds overhead!) diff --git a/src/store/module-example/actions.ts b/src/store/module-example/actions.ts deleted file mode 100644 index 8042ef0..0000000 --- a/src/store/module-example/actions.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { ActionTree } from 'vuex'; -import { StateInterface } from '../index'; -import { ExampleStateInterface } from './state'; - -const actions: ActionTree = { - someAction (/* context */) { - // your code - } -}; - -export default actions; diff --git a/src/store/module-example/mutations.ts b/src/store/module-example/mutations.ts deleted file mode 100644 index 70d45da..0000000 --- a/src/store/module-example/mutations.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { MutationTree } from 'vuex'; -import { ExampleStateInterface } from './state'; - -const mutation: MutationTree = { - someMutation (/* state: ExampleStateInterface */) { - // your code - } -}; - -export default mutation; diff --git a/src/store/module-example/state.ts b/src/store/module-example/state.ts deleted file mode 100644 index 8a9c71e..0000000 --- a/src/store/module-example/state.ts +++ /dev/null @@ -1,9 +0,0 @@ -export interface ExampleStateInterface { - prop: boolean; -} - -const state: ExampleStateInterface = { - prop: false -}; - -export default state; diff --git a/src/store/module-user/actions.ts b/src/store/module-user/actions.ts new file mode 100644 index 0000000..623c2b6 --- /dev/null +++ b/src/store/module-user/actions.ts @@ -0,0 +1,25 @@ +import axios from 'axios'; +import { ActionTree } from 'vuex'; +import { StateInterface } from '../index'; +import { UserStateInterface } from './state'; + +const actions: ActionTree = { + login ({ commit }, payload): any { + axios.post("/auth", { + userid: payload.userid, + password: payload.password + }).then(function (response) { + let token = (response.data).token; + console.log(token); + if (token) + token.expires = new Date(token.expires); + commit('setUser', (response.data).user); + commit('setToken', token); + commit('setPermissions', (response.data).permissions); + }).catch(function (error) { + console.error(error); + }); + } +}; + +export default actions; diff --git a/src/store/module-example/getters.ts b/src/store/module-user/getters.ts similarity index 50% rename from src/store/module-example/getters.ts rename to src/store/module-user/getters.ts index 2727d49..f379c04 100644 --- a/src/store/module-example/getters.ts +++ b/src/store/module-user/getters.ts @@ -1,9 +1,10 @@ import { GetterTree } from 'vuex'; import { StateInterface } from '../index'; -import { ExampleStateInterface } from './state'; +import { UserStateInterface } from './state'; -const getters: GetterTree = { +const getters: GetterTree = { someAction (/* context */) { + console.log("GOOOOOOOOOOOOOOOOOOO"); // your code } }; diff --git a/src/store/module-example/index.ts b/src/store/module-user/index.ts similarity index 68% rename from src/store/module-example/index.ts rename to src/store/module-user/index.ts index fee2203..9bf3797 100644 --- a/src/store/module-example/index.ts +++ b/src/store/module-user/index.ts @@ -1,11 +1,11 @@ import { Module } from 'vuex'; import { StateInterface } from '../index'; -import state, { ExampleStateInterface } from './state'; +import state, { UserStateInterface } from './state'; import actions from './actions'; import getters from './getters'; import mutations from './mutations'; -const exampleModule: Module = { +const exampleModule: Module = { namespaced: true, actions, getters, diff --git a/src/store/module-user/mutations.ts b/src/store/module-user/mutations.ts new file mode 100644 index 0000000..33686cd --- /dev/null +++ b/src/store/module-user/mutations.ts @@ -0,0 +1,16 @@ +import { MutationTree } from 'vuex'; +import { TokenInterface, UserInterface, UserStateInterface } from './state'; + +const mutation: MutationTree = { + setToken (state: UserStateInterface, token: TokenInterface) { + state.token = token + }, + setUser (state: UserStateInterface, user: UserInterface) { + state.user = user + }, + setPermissions (state: UserStateInterface, permissions: Array) { + state.permissions = permissions + } +}; + +export default mutation; diff --git a/src/store/module-user/state.ts b/src/store/module-user/state.ts new file mode 100644 index 0000000..a51bea7 --- /dev/null +++ b/src/store/module-user/state.ts @@ -0,0 +1,29 @@ +export interface TokenInterface { + token: string, + expires: Date, + lifetime: number, + browser: string, + platform: string +} + +export interface UserInterface { + display_name: string | null, + firstname: string, + lastname: string, + mail: string | null, + roles: Array +} + +export interface UserStateInterface { + token: TokenInterface | null, + user: UserInterface | null, + permissions: Array +}; + +const state: UserStateInterface = { + token: null, + user: null, + permissions: [] +}; + +export default state; \ No newline at end of file