diff options
author | jdlugosz963 <jdlugosz963@gmail.com> | 2021-11-09 22:24:46 +0100 |
---|---|---|
committer | jdlugosz963 <jdlugosz963@gmail.com> | 2021-11-09 22:24:46 +0100 |
commit | d1e7d7bf27e25405b6fcdd485e5aa1a3aeb46265 (patch) | |
tree | 3e6d73e7c7f28418afbb700e79ce558717f6b29a /src/store | |
download | lom_frontend-d1e7d7bf27e25405b6fcdd485e5aa1a3aeb46265.tar.gz lom_frontend-d1e7d7bf27e25405b6fcdd485e5aa1a3aeb46265.zip |
Add login system
Diffstat (limited to 'src/store')
-rw-r--r-- | src/store/index.js | 48 |
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 @@ | |||
1 | import Vue from 'vue' | ||
2 | import Vuex from 'vuex' | ||
3 | import Axios from 'axios' | ||
4 | import createPersistedState from "vuex-persistedstate"; | ||
5 | |||
6 | Vue.use(Vuex) | ||
7 | |||
8 | const get_default_state = () => { | ||
9 | return { | ||
10 | token: "", | ||
11 | user: {} | ||
12 | } | ||
13 | } | ||
14 | |||
15 | const 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 | |||
48 | export default store | ||