justyeh / nuxt.justyeh.top

Vue.js+Nuxt.js+Es6+Webpack+Mysql+Noyde.js+Express.js构建的SSR博客网站
162 stars 60 forks source link

提问中间件问题 #3

Open EasonShen1989 opened 7 years ago

EasonShen1989 commented 7 years ago

只在客户端 不通过服务端 是否可以判断登录状态

justyeh commented 7 years ago

这个应该是做不到的,只有服务端才能存有和校验用户的验证信息

justyeh@163.com

发件人: 申飞 发送时间: 2017-07-29 17:00 收件人: justyeh/justyeh.com 抄送: Subscribed 主题: [justyeh/justyeh.com] 提问中间件问题 (#3) 只在客户端 不通过服务端 是否可以判断登录状态 — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

EasonShen1989 commented 7 years ago

1、middleware import Vue from 'vue'

export default ({ store, redirect, route }) => { if (!store.getters['authorization/isAuthenticated'] && route.path.indexOf('/login') < 0) { return redirect('/login') } }

2.store export const getters = { isAuthenticated(state) { if (!Vue.prototype.$isServer) { return !!state.value || !!global.localStorage.getItem('authentication') } } }

3.不刷新是可以的 但是每次刷新就跳转到登录页面(刷新时store.getters['authorization/isAuthenticated']拿不到不刷新可以的)

justyeh commented 7 years ago

你检查一下登录成功后authentication有没有登录存到localStorage中