Skip to content

Commit

Permalink
add vuex auth
Browse files Browse the repository at this point in the history
  • Loading branch information
kun committed Sep 2, 2019
1 parent 81926af commit ac32a63
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
6 changes: 4 additions & 2 deletions middleware/default.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export default function ({ route }) {
console.log(111);
export default function ({ route, store, redirect }) {
// 如果用户不存在,跳到登录页面
if (!store.state.user.user)
redirect('/user/login?redirect=' + route.path);
}
4 changes: 2 additions & 2 deletions pages/info/_pageIndex.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export default {
});
return { ...data.data };
},
middleware: "default",
components: {
vmenu: Menu,
vpage: page
Expand All @@ -48,8 +49,7 @@ export default {
pageIndex: this.$route.params.pageIndex || 1
};
},
async created() {
}
async created() {}
};
</script>

Expand Down
8 changes: 8 additions & 0 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ const config = require('../nuxt.config.js')
config.dev = !(app.env === 'production')


// test session
app.use(async (ctx, next) => {
ctx.req.session = {
user: { name: 'test' }
};
await next();
})

app.use(bodyParser());

app.use(router.routes());
Expand Down
8 changes: 8 additions & 0 deletions store/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

export const actions = {
nuxtServerInit({ commit }, { req }) {
if (req.session.user) {
commit("user/set", req.session.user);
}
}
}
14 changes: 14 additions & 0 deletions store/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

export const state = () => {
user: null
};

export const mutations = {
// 设置登录用户
set(state, user) {
state.user = user;
},
loginOut(state) {
state.user = null;
}
}

0 comments on commit ac32a63

Please sign in to comment.