nuxt.justyeh.top icon indicating copy to clipboard operation
nuxt.justyeh.top copied to clipboard

提问中间件问题

Open EasonShen1989 opened this issue 7 years ago • 3 comments

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

EasonShen1989 avatar Jul 29 '17 09:07 EasonShen1989

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

[email protected]

发件人: 申飞 发送时间: 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.

justyeh avatar Jul 29 '17 09:07 justyeh

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']拿不到不刷新可以的)

EasonShen1989 avatar Jul 29 '17 09:07 EasonShen1989

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

justyeh avatar Jul 29 '17 09:07 justyeh