From 1ad39f386ebde61c75aa623d063c1b92b4810180 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Thu, 15 Oct 2020 11:23:41 +0200 Subject: [PATCH 1/2] transfer to mac --- src/boot/axios.ts | 2 ++ src/plugins/user/models.ts | 4 +++ src/plugins/user/store/user.ts | 52 ++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 src/plugins/user/models.ts create mode 100644 src/plugins/user/store/user.ts diff --git a/src/boot/axios.ts b/src/boot/axios.ts index d8ed8d2..15375ba 100644 --- a/src/boot/axios.ts +++ b/src/boot/axios.ts @@ -11,3 +11,5 @@ export default boot(({ Vue }) => { // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access Vue.prototype.$axios = axios; }); + +export { axios }; diff --git a/src/plugins/user/models.ts b/src/plugins/user/models.ts new file mode 100644 index 0000000..64149db --- /dev/null +++ b/src/plugins/user/models.ts @@ -0,0 +1,4 @@ +export interface LoginData { + userid: string; + password: string; +} diff --git a/src/plugins/user/store/user.ts b/src/plugins/user/store/user.ts new file mode 100644 index 0000000..54a0839 --- /dev/null +++ b/src/plugins/user/store/user.ts @@ -0,0 +1,52 @@ +import { Module, MutationTree, ActionTree, Commit } from 'vuex'; +import { StateInterface } from 'src/store'; +import { axios } from 'boot/axios'; +import { LoginData } from 'src/plugins/user/models' + +export interface Token { + browser: string; + expires: string; + lifetime: number; + platform: string; + token: string; +} + +export interface User { + display_name: string | null; + firstname: string; + lastname: string; + mail: string; + roles: string[]; +} + +export interface UserStateInterface { + permissions: string[]; + token: Token; + user: User; + userid: string; +} + +const state: UserStateInterface = { + permissions: [], + token: { browser: '', expires: '', lifetime: -1, platform: '', token: '' }, + user: { + display_name: '', + firstname: '', + lastname: '', + mail: '', + roles: [] + }, + userid: '' +}; + +const mutation: MutationTree = { + setState(state: UserStateInterface, data: UserStateInterface) { + state = data; + } +}; + +const actions: ActionTree = { + async = login({ commit: Commit }, data: LoginData) { + axios + } +}; From 8409e09f191e42d215f16bb8e22a18c18935752f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Fri, 16 Oct 2020 08:45:40 +0200 Subject: [PATCH 2/2] login --- src/boot/plugins.ts | 21 ++++--- src/pages/Login.vue | 13 +++- src/plugins/user/pages/Settings.vue | 42 +++++++++++++ src/plugins/user/pages/User.vue | 17 +++++- src/plugins/user/plugin.ts | 18 +++++- src/plugins/user/routes/index.ts | 9 ++- src/plugins/user/store/session.ts | 68 +++++++++++++++++++++ src/plugins/user/store/user.ts | 92 ++++++++++++++++++++++++++--- src/router/index.ts | 25 ++++---- 9 files changed, 267 insertions(+), 38 deletions(-) create mode 100644 src/plugins/user/pages/Settings.vue create mode 100644 src/plugins/user/store/session.ts diff --git a/src/boot/plugins.ts b/src/boot/plugins.ts index fc0ed43..f8bf787 100644 --- a/src/boot/plugins.ts +++ b/src/boot/plugins.ts @@ -1,12 +1,12 @@ import { boot } from 'quasar/wrappers'; import { RouteConfig } from 'vue-router'; -import { Module } from 'vuex'; +import { Module, Store } from 'vuex'; const config = { // Do not change required Modules !! requiredModules: ['user'], // here you can import plugins. - loadModules: ['plugin1', 'user-plugin'] + loadModules: [] }; // do not change anything here !! @@ -19,7 +19,7 @@ interface ShortCutLink { interface Plugin { name: string; routes: RouteConfig[]; - store?: Module[]; + store?: Map>; mainLink: PluginMainLink; requiredModules: string[]; shortcuts: ShortCutLink[]; @@ -65,7 +65,7 @@ function combineRoutes( target: RouteConfig[], source: RouteConfig[] ): RouteConfig[] { - // iterate first layer e.g. /main, /login etc. + // iterate first layer e.g. /main, / etc. source.forEach((sourceRouteConfig: RouteConfig) => { const targetRouteConfig: RouteConfig | undefined = target.find( (routeConfig: RouteConfig) => { @@ -138,7 +138,7 @@ function loadPlugin( loadedPlugins: LoadedPlugins, modules: string[], plugins: Plugin[], - store: any + store: Store ): LoadedPlugins { modules.forEach(requiredModule => { const plugin = plugins.find(plugin => { @@ -147,10 +147,15 @@ function loadPlugin( if (plugin) { loadedPlugins.routes = combineRoutes(loadedPlugins.routes, plugin.routes); if (plugin.store) { - plugin.store.forEach(store_module => { - // eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access - store.registerModule(store_module); + console.log(plugin.store); + console.log(plugin.store.keys()); + plugin.store.forEach((store_plugin, store_namespace) => { + store.registerModule(store_namespace, store_plugin); }); + //.forEach(store_key => { + // eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access + //store.registerModule(store_key, plugin.store.get(store_key)); + //}); } loadedPlugins.mainLinks = combineMainLinks( loadedPlugins.mainLinks, diff --git a/src/pages/Login.vue b/src/pages/Login.vue index 66e3199..bd5fa0e 100644 --- a/src/pages/Login.vue +++ b/src/pages/Login.vue @@ -25,6 +25,7 @@ :rules="rules" />
+
@@ -47,10 +48,18 @@ export default defineComponent({ function doLogin() { console.log(username.value, password.value); - void ctx.root.$router.push({ name: 'main' }); + ctx.root.$store.dispatch('user/login', { + userid: username.value, + password: password.value + }); + //ctx.root.$router.push({ name: 'main' }); } - return { username, password, doLogin, rules }; + function showStore() { + console.log(ctx.root.$store); + } + + return { username, password, doLogin, rules, showStore }; } }); diff --git a/src/plugins/user/pages/Settings.vue b/src/plugins/user/pages/Settings.vue new file mode 100644 index 0000000..4946dcb --- /dev/null +++ b/src/plugins/user/pages/Settings.vue @@ -0,0 +1,42 @@ + + + diff --git a/src/plugins/user/pages/User.vue b/src/plugins/user/pages/User.vue index 5acf604..ab9f24f 100644 --- a/src/plugins/user/pages/User.vue +++ b/src/plugins/user/pages/User.vue @@ -7,22 +7,33 @@ > - {{ mainLink.title }} + {{ `Hallo ${firstname} ${lastname}` }} + diff --git a/src/plugins/user/plugin.ts b/src/plugins/user/plugin.ts index 3bef5f6..ebfb36f 100644 --- a/src/plugins/user/plugin.ts +++ b/src/plugins/user/plugin.ts @@ -1,4 +1,7 @@ import { Plugin, PluginMainLink } from 'boot/plugins'; +import { Module } from 'vuex'; +import userStore from './store/user'; +import sessionsStore from './store/session'; import routes from './routes'; const mainLink: PluginMainLink = { @@ -6,7 +9,14 @@ const mainLink: PluginMainLink = { title: 'User', link: 'user', icon: 'mdi-account', - children: [] + children: [ + { + name: 'user', + title: 'Einstellungen', + link: 'user-settings', + icon: 'mdi-cog' + } + ] }; const plugin: Plugin = { @@ -16,7 +26,11 @@ const plugin: Plugin = { requiredModules: [], shortcutsOut: [], shortcuts: [], - version: '0.0.1' + version: '0.0.1', + store: new Map>([ + ['user', userStore], + ['sessions', sessionsStore] + ]) }; export { mainLink }; diff --git a/src/plugins/user/routes/index.ts b/src/plugins/user/routes/index.ts index e176de9..50a73f9 100644 --- a/src/plugins/user/routes/index.ts +++ b/src/plugins/user/routes/index.ts @@ -8,7 +8,14 @@ const routes: RouteConfig[] = [ { path: 'user', name: 'user', - component: () => import('../pages/User.vue') + component: () => import('../pages/User.vue'), + children: [ + { + path: 'settings', + name: 'user-settings', + component: () => import('../pages/Settings.vue') + } + ] } ] } diff --git a/src/plugins/user/store/session.ts b/src/plugins/user/store/session.ts new file mode 100644 index 0000000..8a6112c --- /dev/null +++ b/src/plugins/user/store/session.ts @@ -0,0 +1,68 @@ +import { Module, MutationTree, ActionTree, GetterTree } from 'vuex'; +import { StateInterface } from 'src/store'; +import { axios } from 'boot/axios'; +import { LoginData } from 'src/plugins/user/models'; +import { AxiosResponse } from 'axios'; +import { LocalStorage } from 'quasar'; +import { Router } from 'src/router'; + +export interface SessionInterface { + sessions: Session[]; + loading: boolean; +} +export interface Session { + browser: string; + expires: string; + lifetime: number; + platform: string; + token: string; +} +const state: SessionInterface = { + sessions: [], + loading: false +}; + +const mutations: MutationTree = { + setSessions(state, sessions: Session[]) { + state.sessions = sessions; + }, + setLoading(state, value) { + state.loading = value; + } +}; + +const actions: ActionTree = { + getSessions({ commit, rootGetters }) { + console.log(rootGetters); + commit('setLoading', true); + axios + .get('http://localhost:5000/auth', { + headers: { Token: rootGetters['user/token'].token } + }) + .then(response => { + commit('setSessions', response.data); + }) + .catch(error => { + console.exception(error); + }) + .finally(() => { + commit('setLoading', false); + }); + } +}; + +const getters: GetterTree = { + sessions(state) { + return state.sessions; + } +}; + +const sessions: Module = { + namespaced: true, + state, + mutations, + actions, + getters +}; + +export default sessions; diff --git a/src/plugins/user/store/user.ts b/src/plugins/user/store/user.ts index 54a0839..5762565 100644 --- a/src/plugins/user/store/user.ts +++ b/src/plugins/user/store/user.ts @@ -1,7 +1,10 @@ -import { Module, MutationTree, ActionTree, Commit } from 'vuex'; +import { Module, MutationTree, ActionTree, GetterTree } from 'vuex'; import { StateInterface } from 'src/store'; import { axios } from 'boot/axios'; -import { LoginData } from 'src/plugins/user/models' +import { LoginData } from 'src/plugins/user/models'; +import { AxiosResponse } from 'axios'; +import { LocalStorage } from 'quasar'; +import { Router } from 'src/router'; export interface Token { browser: string; @@ -19,7 +22,11 @@ export interface User { roles: string[]; } -export interface UserStateInterface { +export interface UserStateInterface extends LoginResponse { + loginLoading: boolean; +} + +export interface LoginResponse { permissions: string[]; token: Token; user: User; @@ -36,17 +43,84 @@ const state: UserStateInterface = { mail: '', roles: [] }, - userid: '' + userid: '', + loginLoading: false }; -const mutation: MutationTree = { - setState(state: UserStateInterface, data: UserStateInterface) { - state = data; +const mutations: MutationTree = { + setPermissions(state, data: []) { + state.permissions = data; + }, + setToken(state, data: Token) { + state.token = data; + }, + setUser(state, data: User) { + state.user = data; + }, + setUserId(state, data: string) { + state.userid = data; + }, + setLoginLoading(state, data: boolean) { + state.loginLoading = data; + }, + showState(state) { + console.log(state); } }; const actions: ActionTree = { - async = login({ commit: Commit }, data: LoginData) { - axios + login({ commit }, data: LoginData) { + console.log('bla'); + commit('setLoginLoading', true); + void axios + .post('http://localhost:5000/auth', data) + .then((response: AxiosResponse) => { + commit('setPermissions', response.data.permissions); + console.log('saved permisisons'); + commit('setToken', response.data.token); + commit('setUser', response.data.user); + commit('setUserId', response.data.userid); + commit('showState'); + //LocalStorage.set('permissions', response.data.permissions); + //LocalStorage.set('token', response.data.token); + //LocalStorage.set('user', response.data.user); + //LocalStorage.set('userid', response.data.userid); + + void Router.push({ name: 'user' }); + }) + .catch(error => { + console.exception(error); + }) + .finally(() => { + commit('setLoginLoading', false); + }); } }; + +const getters: GetterTree = { + permissions({ permissions }) { + return permissions; + }, + token({ token }) { + return token; + }, + user({ user }) { + return user; + }, + userid({ userid }) { + return userid; + }, + loginLoading({ loginLoading }) { + return loginLoading; + } +}; + +const userStore: Module = { + namespaced: true, + actions, + getters, + mutations, + state +}; + +export default userStore; diff --git a/src/router/index.ts b/src/router/index.ts index b6d4373..b70714d 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -8,20 +8,19 @@ import routes from './routes'; * If not building with SSR mode, you can * directly export the Router instantiation */ +export const Router: VueRouter = new VueRouter({ + scrollBehavior: () => ({ x: 0, y: 0 }), + routes, -export default route>(function ({ Vue }) { + // Leave these as is and change from quasar.conf.js instead! + // quasar.conf.js -> build -> vueRouterMode + // quasar.conf.js -> build -> publicPath + mode: process.env.VUE_ROUTER_MODE, + base: process.env.VUE_ROUTER_BASE +}); + +export default route>(function({ Vue }) { Vue.use(VueRouter); - const Router = new VueRouter({ - scrollBehavior: () => ({ x: 0, y: 0 }), - routes, - - // Leave these as is and change from quasar.conf.js instead! - // quasar.conf.js -> build -> vueRouterMode - // quasar.conf.js -> build -> publicPath - mode: process.env.VUE_ROUTER_MODE, - base: process.env.VUE_ROUTER_BASE - }); - return Router; -}) +});