Skip to content

Commit 637e656

Browse files
committed
store logged in user in vuex persisted in localstorage
1 parent e006372 commit 637e656

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

frontend/src/store/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
11
import Vue from 'vue';
22
import Vuex from 'vuex';
3+
import createPersistedState from 'vuex-persistedstate';
4+
import SecureLS from 'secure-ls';
5+
import user from './modules/user';
6+
7+
const ls = new SecureLS({ isCompression: false });
38

49
Vue.use(Vuex);
510

611
export default new Vuex.Store({
12+
strict: true,
713
state: {
814
},
915
mutations: {
1016
},
1117
actions: {
1218
},
1319
modules: {
20+
user,
1421
},
22+
plugins: [
23+
createPersistedState({
24+
paths: ['user'],
25+
storage: {
26+
getItem: (key) => ls.get(key),
27+
setItem: (key, value) => ls.set(key, value),
28+
removeItem: (key) => ls.remove(key),
29+
},
30+
}),
31+
],
1532
});

frontend/src/store/modules/user.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* eslint-disable no-shadow */
2+
3+
const state = {
4+
user: null,
5+
};
6+
7+
const getters = {
8+
getLoggedInUser(state) {
9+
return state.user;
10+
},
11+
};
12+
13+
const mutations = {
14+
login(state, user) {
15+
state.user = user;
16+
},
17+
logout(state) {
18+
state.user = null;
19+
},
20+
};
21+
22+
const actions = {
23+
login(state, user) {
24+
state.commit('login', user);
25+
},
26+
logout(state) {
27+
state.commit('logout');
28+
},
29+
};
30+
31+
export default {
32+
state,
33+
getters,
34+
mutations,
35+
actions,
36+
};

0 commit comments

Comments
 (0)