From 0512ac2c3ce7933c1da051edf1bb05d7cb93b8e6 Mon Sep 17 00:00:00 2001 From: "D. Berge" Date: Sun, 11 Oct 2020 17:53:39 +0200 Subject: [PATCH] Add `user` module to Vuex store --- lib/www/client/source/src/store/index.js | 2 + .../source/src/store/modules/user/actions.js | 41 +++++++++++++++++++ .../source/src/store/modules/user/getters.js | 6 +++ .../source/src/store/modules/user/index.js | 6 +++ .../src/store/modules/user/mutations.js | 10 +++++ .../source/src/store/modules/user/state.js | 6 +++ 6 files changed, 71 insertions(+) create mode 100644 lib/www/client/source/src/store/modules/user/actions.js create mode 100644 lib/www/client/source/src/store/modules/user/getters.js create mode 100644 lib/www/client/source/src/store/modules/user/index.js create mode 100644 lib/www/client/source/src/store/modules/user/mutations.js create mode 100644 lib/www/client/source/src/store/modules/user/state.js diff --git a/lib/www/client/source/src/store/index.js b/lib/www/client/source/src/store/index.js index 4b643bb..12ece78 100644 --- a/lib/www/client/source/src/store/index.js +++ b/lib/www/client/source/src/store/index.js @@ -2,6 +2,7 @@ import Vue from 'vue' import Vuex from 'vuex' import api from './modules/api' +import user from './modules/user' import snack from './modules/snack' import project from './modules/project' import notify from './modules/notify' @@ -11,6 +12,7 @@ Vue.use(Vuex) export default new Vuex.Store({ modules: { api, + user, snack, project, notify diff --git a/lib/www/client/source/src/store/modules/user/actions.js b/lib/www/client/source/src/store/modules/user/actions.js new file mode 100644 index 0000000..28077a8 --- /dev/null +++ b/lib/www/client/source/src/store/modules/user/actions.js @@ -0,0 +1,41 @@ +import jwt_decode from 'jwt-decode'; + +async function login ({commit, dispatch}, loginRequest) { + const url = "/login"; + const init = { + method: "POST", + headers: { + "Content-Type": "application/json" + }, + body: loginRequest + } + const res = await dispatch('api', [url, init]); + if (res && res.ok) { + dispatch('setCredentials', true); + } +} + +async function logout ({commit, dispatch}) { + commit('setCookie', null); + commit('setUser', null); + await dispatch('api', ["/logout"]); + // Should delete JWT cookie +} + +function browserCookie (state) { + return document.cookie.split(/; */).find(i => i.startsWith("JWT=")); +} + +function cookieChanged (cookie) { + return browserCookie != cookie; +} + +function setCredentials ({state, commit, getters, dispatch}, force = false) { + if (cookieChanged(state.cookie) || force) { + const cookie = browserCookie(); + commit('setCookie', cookie); + commit('setUser', cookie ? jwt_decode(cookie.split("=")[1]) : null); + } +} + +export default { login, logout, setCredentials }; diff --git a/lib/www/client/source/src/store/modules/user/getters.js b/lib/www/client/source/src/store/modules/user/getters.js new file mode 100644 index 0000000..2d74f30 --- /dev/null +++ b/lib/www/client/source/src/store/modules/user/getters.js @@ -0,0 +1,6 @@ + +function user (state) { + return state.user; +} + +export default { user }; diff --git a/lib/www/client/source/src/store/modules/user/index.js b/lib/www/client/source/src/store/modules/user/index.js new file mode 100644 index 0000000..dae701e --- /dev/null +++ b/lib/www/client/source/src/store/modules/user/index.js @@ -0,0 +1,6 @@ +import state from './state' +import getters from './getters' +import actions from './actions' +import mutations from './mutations' + +export default { state, getters, actions, mutations }; diff --git a/lib/www/client/source/src/store/modules/user/mutations.js b/lib/www/client/source/src/store/modules/user/mutations.js new file mode 100644 index 0000000..79ce4cb --- /dev/null +++ b/lib/www/client/source/src/store/modules/user/mutations.js @@ -0,0 +1,10 @@ + +function setCookie (state, cookie) { + state.cookie = cookie; +} + +function setUser (state, user) { + state.user = user; +} + +export default { setCookie, setUser }; diff --git a/lib/www/client/source/src/store/modules/user/state.js b/lib/www/client/source/src/store/modules/user/state.js new file mode 100644 index 0000000..6b17733 --- /dev/null +++ b/lib/www/client/source/src/store/modules/user/state.js @@ -0,0 +1,6 @@ +const state = () => ({ + cookie: null, + user: null, +}); + +export default state;