Skip to content

Commit 3b6ac85

Browse files
author
Riley Ren
committed
replace localForage with cookies
1 parent 95df8e1 commit 3b6ac85

File tree

2 files changed

+15
-31
lines changed

2 files changed

+15
-31
lines changed

src/router/index.js

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,22 @@ const routes = [
2323
...chargeOff,
2424
];
2525

26+
store.dispatch('sign/restoreSign');
27+
2628
const router = new Router({
2729
routes,
2830
});
2931

3032
router.beforeEach((to, from, next) => {
31-
if(!store.getters['sign/restored']) {
32-
store.dispatch('sign/restoreSign').then(function(signed) {
33-
// if (to.matched.some(r => r.meta.requireAuth)) {
34-
if (to.meta.requireAuth && !store.getters['sign/signed']) {
35-
store.commit('sign/' + types.SHOW_SIGN_IN_MODAL);
36-
next({
37-
path: '/',
38-
// query: {redirect: to.fullPath}
39-
});
40-
} else {
41-
next();
42-
}
43-
}).catch((ex) => {});
33+
// if (to.matched.some(r => r.meta.requireAuth)) {
34+
if (to.meta.requireAuth && !store.getters['sign/signed']) {
35+
store.commit('sign/' + types.SHOW_SIGN_IN_MODAL);
36+
next({
37+
path: '/',
38+
// query: {redirect: to.fullPath}
39+
});
4440
} else {
45-
if (to.meta.requireAuth && !store.getters['sign/signed']) {
46-
store.commit('sign/' + types.SHOW_SIGN_IN_MODAL);
47-
next({
48-
path: '/',
49-
// query: {redirect: to.fullPath}
50-
});
51-
} else {
52-
next();
53-
}
41+
next();
5442
}
5543
});
5644

src/store/modules/sign.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import localForage from 'localforage';
1+
import Cookies from 'js-cookie';
22
import Api from '@/api';
33
import * as types from '../mutation-types';
44

@@ -8,35 +8,32 @@ import * as types from '../mutation-types';
88
const state = {
99
signed: false,
1010
showModal: false,
11-
restored: false,
1211
};
1312

1413
// getters
1514
const getters = {
1615
signed: state => state.signed,
17-
restored: state => state.restored,
1816
showModal: state => state.showModal
1917
};
2018

2119
// actions
2220
const actions = {
2321
async signIn({ commit }, { user, password }) {
2422
let ret = await Api.sign.signIn(user, password);
25-
// let signed = ret.signed;
2623
let signed = true;
27-
await localForage.setItem('signed', signed);
24+
Cookies.set('signed', 1);
2825
commit(types.SIGN_IN, { signed });
2926
},
30-
async restoreSign({ commit }) {
31-
let signed = await localForage.getItem('signed');
27+
restoreSign({ commit }) {
28+
let signed = parseInt(Cookies.get('signed'), 10);
3229
if(signed) {
3330
commit(types.RESTORE_SIGN, { signed });
3431
}
3532
return signed;
3633
},
3734
async signOut({ commit }) {
3835
let ret = await Api.sign.signOut();
39-
await localForage.removeItem('signed');
36+
Cookies.remove('signed');
4037
commit(types.SIGN_OUT);
4138
}
4239
};
@@ -48,7 +45,6 @@ const mutations = {
4845
},
4946
[types.RESTORE_SIGN](state, { signed }) {
5047
state.signed = signed;
51-
state.restored = true;
5248
},
5349
[types.SIGN_OUT](state) {
5450
state.signed = false;

0 commit comments

Comments
 (0)