summaryrefslogtreecommitdiffstats
path: root/src/store
diff options
context:
space:
mode:
authorjdlugosz963 <jdlugosz963@gmail.com>2021-11-09 22:24:46 +0100
committerjdlugosz963 <jdlugosz963@gmail.com>2021-11-09 22:24:46 +0100
commitd1e7d7bf27e25405b6fcdd485e5aa1a3aeb46265 (patch)
tree3e6d73e7c7f28418afbb700e79ce558717f6b29a /src/store
downloadlom_frontend-d1e7d7bf27e25405b6fcdd485e5aa1a3aeb46265.tar.gz
lom_frontend-d1e7d7bf27e25405b6fcdd485e5aa1a3aeb46265.zip
Add login system
Diffstat (limited to 'src/store')
-rw-r--r--src/store/index.js48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/store/index.js b/src/store/index.js
new file mode 100644
index 0000000..fa8a1bd
--- /dev/null
+++ b/src/store/index.js
@@ -0,0 +1,48 @@
1import Vue from 'vue'
2import Vuex from 'vuex'
3import Axios from 'axios'
4import createPersistedState from "vuex-persistedstate";
5
6Vue.use(Vuex)
7
8const get_default_state = () => {
9 return {
10 token: "",
11 user: {}
12 }
13}
14
15const store = new Vuex.Store({
16 plugins: [createPersistedState()],
17 state: get_default_state(),
18
19 mutations: {
20 set_token: (state, token) => state.token = token,
21 set_user: (state, user) => state.user = user,
22 reset_state: (state) => Object.assign(state, get_default_state())
23 },
24
25 getters: {
26 is_logged_in: (state) => {
27 return state.token !== ""
28 },
29
30 get_user: (state) => {
31 return state.user
32 }
33 },
34
35 actions: {
36 login: ({commit}, {token, user}) => {
37 commit('set_token', token)
38 commit('set_user', user)
39
40 Axios.defaults.headers.common['Authorization'] = `Token ${token}`;
41 },
42 logout: ({commit}) => {
43 commit('reset_state')
44 }
45 }
46})
47
48export default store