使用Vuex来处理Authentication token
https://www.cnblogs.com/chentianwei/p/10156459.html
之前博客:建立了一个app。使用loacal storage 来管理tokens(通过client-side anuthentication 生成)。
问题是这种方法有些麻烦。
每次要检验用户是否通过验证,都需要先储存auth token和user信息进localStorage,然后在从中取出来进行各种各样的条件判断,boring。
this.$http.post(url, {
//...略
})
.then(response => {
localStorage.setItem('user', JSON.stringify(response.data.user))
localStorage.setItem('jwt', response.data.token) if (localStorage.getItem('jwt') != null) {
//...略
}
})//
可以改用Vuex。
使用store来管理authentication state, 然后只需使用少许代码即可检查我们的application.
main.js引进axios
import Axios from 'axios' Vue.prototype.$http = Axios
const token = localStorage.getItem('token')
if (token) {
Vue.prototype.$http.defaults.headers.common['Authorization'] = token
}
We also set the Authorization
on axios header to our token, so our requests can be processed if a token is required. This way, we do not have to set token anytime we want to make a request.
默认设置,所有请求都会配置到:
给axios的headers赋值token 。这样,无需在每一次想要发出请求时,都设置token。
store.js解释
state存储
authentication status, 用于当使用mutation时,status数据储存验证过程的状态:pending,success, error。
jwt token
user信息
actions中的函数
login函数:
login({commit}, user) {
return new Promise((resolve, reject) => {
commit('auth_request')
// 这是其中一种类似config的写法,也可以使用axios.post('', {...})
axios({url: 'http://localhost:3000/login', data:user, method: 'POST'})
.then(resp => {
const token = resp.data.token
const user = resp.data.user
localStorage.setItem('token', token)
//main.js中设置了Vue.prototype.$http = Axios
axios.defualts.headers.common['Authorization'] = token
commit('auth_success', token, user)
// 返回一个状态由resp决定的Promise对象。
resolve(resp)
})
.catch(err => {
commit('auth_error')
localStorage.removeItem('token')
// 返回一个状态为失败的Promise对象
reject(err)
})
})
},
这里使用new Promise构造器返回一个promise对象。
login action用于验证一个user,并commit用户的credentials到vuex store。
- 使用axios调用服务器的login路由,并返回需要的data。
- 然后储存token到localStorage,
- 然后传递token和user的信息到auth_success mutation来更新store.state中的属性。
- 同时,也设置axios的headers。
axios.defaults.headers.common['Authorization'] = token
⚠️ token不要储存在vuex store中,一旦离开程序,所有的Vuex.store中的data都会消失。 因此,为了确保用户返回到程序后,只要token处于验证期内, 就无需让user再登陆一次。 需要把token储存再localStorage, 即本地设备中。
最后返回promise对象, 以便在login完成后,能够返回一个response给user。
知识扩展:
Promise对象及构造函数
Promise对象用于表示一个异步操作的最终状态(完成或失败),以及返回的值。
new Promise产生的构造函数用于包装还未支持promises的函数。
const myFirstPromise = new Promise((resolve, reject) => {
// ?做一些异步操作,最终会调用下面两者之一:
//
// resolve(someValue); // fulfilled
// ?或
// reject("failure reason"); // rejected
});
例子:
var promisel = new Promise(function(resolve, reject) {
//当异步代码执行成功后,调用resolve(),当异步代码执行失败后,调用reject()
//本例子使用setTimout()来模拟异步代码
//真实环境,通常使用HTMl5 API,或者如XHR。
setTimeout(function() {
resolve('Success!') //Yes! Everything went well!
}, 300);
}) promisel.then(function(value) {
console.log(value)
//控制台输出的是"Success"
})
描述:
Promise
对象是一个代理对象(代理一个值), 被代理的值在Promise对象创建时可能是未知的。
Promise is a proxy for a value not necessarily known when the promise is created。
它允许你为异步操作的成功和失败分别绑定相应的处理方法(handlers)。
It allows you to associate handlers with an asynchronous action's success value or failure reason.
这让异步方法可以像同步方法那样返回值,但并不是立即返回最终执行结果,而是一个能代表未来出现的结果的promise对象。
一个Promise有3种状态:
- pending, 之后会导致成功或失败:
- fulfilled
- rejected
Promise.reject(reason)
- 返回一个状态为失败的Promise对象,并将给定的失败信息传递给对应的处理方法
Promise.resolve(value)
- 返回一个状态由给定value决定的Promise对象。
创建register action
和login action代码基本一样。区别是aixos.post的url路由不一样。
它们都会commit('auth_request'), commit('auth_success')因为这两个action的目标都是把user信息存入state。
创建logout action
当登出后,删除储存在本地设备的token。
同时使用delete方法删除Vue实例对象的属性。 这里用delete axios的headers。
logout({commit}) {
return new Promise((resolve, reject) => {
commit('logout')
localStorage.removeItem('token')
// 使用Vue.delete(target), target可以是对象或数组。
delete axios.defaults.headers.common['Authorization']
resolve()
})
}
Hide Pages Behind Auth
本文的目的时implement anthentication,未通过验证的用户不能访问某些页面。
为了实现这个目的,需要弄明白:
1. 用户想访问的页面,应当有一个方法可以检查是否用户是通过验证的。
2.一个页面是专为验证过的用户访问的,还是所有用户都可以访问。
使用vue-router可以解决上面的2个问题
vue-router的导航守卫和路由meta信息可以对从当前url到另一个url的过程进行全程的控制。
- meta对路由记录进行条件设置
- navigation Guard对整个导航过程进行控制,根据meta进行判断是否跳转或者取消导航。
首先router.js:
import Vue from 'vue'
import Router from 'vue-router'
import store from './store.js'
import Home from './views/Home.vue'
import About from './views/About.vue'
import Login from './components/Login.vue'
import Secure from './components/Secure.vue'
import Register from './components/Register.vue' Vue.use(Router)
引进vue, vue-router, 和vuex store setup.
所有的组件也被引进,用于定义路由。
然后,使用meta属性。
[...]
let router = new Router({
mode: 'history',
routes: [
//...略过home, login, register
{
path: '/secure',
name: 'secure',
component: Secure,
meta: {
requiresAuth: true
}
},
//...略过about
]
}) export default router/
使用router.beforeEach注册一个全局前置守卫:
router.beforeEach((to,from,next) => {
// 要前往的路由是否有meta条件:
// 是,进一步判断
// 否,直接跳转到这个路由。
if (to.matched.some(record => record.meta.requiresAuth)) {
// 判断用户是否有权限进入这个页面:(使用了vuex store)
if (store.getters.isLoggedIn) {
next()
} else {
next('/login')
}
} else {
next()
}
})
给路由记录增加各类条件(使用meta),然后用Navigation Guards来进行判断后,取消或者跳转。
这里还使用了VuexStore.getters中的方法,去掉了重复的条件判断。
通过Vuex store,我们能够定义actions来检查路由记录中的条件,定义getters返回这些条件的结果。
处理到期的Token
因为储存token在本地localStorage,token会永久保存。
如果它已经到期,那么我们的请求会因为invalid token而失败
打开./src/App.vue,增加一个hook:
export default {
[...]
created: function () {
this.$http.interceptors.response.use(undefined, function (err) {
return new Promise(function (resolve, reject) {
if (err.status === 401 && err.config && !err.config.__isRetryRequest) {
this.$store.dispatch(logout)
}
throw err;
});
});
}
}
拦截收到的response,看是否得到的是401 Unauthorized response。
如果是401,则dispatch logout action,让本地token被删除,然后跳转到login页面,重新登陆!
如此改善了用户体验!
使用Vuex来处理Authentication token的更多相关文章
- 解决修改密码报错‘passwd:Authentication token’
1.修改密码时报错: 错误信息:'passwd: Authentication token manipulation error' [root@localhost test]# ' | passwd ...
- vmware 解决 authentication token manipulation error
vmvare虚拟机长时间未使用,导致再次登录的时候密码忘了,无法登录. 启动时长按shift,进入root(recovery)模式, (recovery mode),进入"Recovery ...
- 无法修改linux/ubuntu密码(Authentication token manipulation error )问题解决过程【转】
转自:https://blog.csdn.net/caizi001/article/details/38659189 Vmware虚拟机里的ubunut系统长期不用,密码忘记了,无奈只能通过slax ...
- Linux学习笔记之passwd:Authentication token manipulation error_错误的解决办法
如果在linux中,不管是root用户还是普通用户登录后,修改自己的密码,出现—passwd:Authentication token manipulation error—错误的解决办法: root ...
- linux环境,crontab报错Authentication token is no longer valid; new one required You (aimonitor) are not allowed to access to (crontab) because of pam configuration.
问题描述: 今天同事反应,一个系统上的某些数据没有生成,看了下,怀疑定时任务没有执行,就看下了crontab,发现报了下面的错误: [aimonitor@4A-LF-w08 ~]$ crontab - ...
- 解决"authentication token manipulation error"
昨天安装是Ubuntu Server. 配置好了几个软件后就忘记继续了...今天打开,居然忘记了密码...真是的.. 后来还是要改了. 不想重新弄什么的了..百度了下怎么改密码...然后就有一篇 ...
- linux修改密码出现Authentication token manipulation error的解决办法
转自 :http://blog.163.com/junwu_lb/blog/static/1916798920120103647199/ Authentication token manipulati ...
- Authentication token is no longer valid
Linux: Authentication token is no longer valid Problem: Authentication token is no longer valid; new ...
- authentication token manipulation error
用户服务器中修改密码,输入passwd命令后,报错authentication token manipulation error 发生该错误原因是: 1.分区没有空间导致. 2./etc/pass ...
随机推荐
- ODAC(V9.5.15) 学习笔记(四)TMemDataSet (3)
3.其他 名称 类型 说明 GetBlob TBlob 按照字段名获取当前数据集中某个Blob类型的字段值,并以TBlob对象形式返回 Prepared Boolean 检查Query的SQL是否已准 ...
- Python3基础 list [] 创建整数列表
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- windows进程中的几个杂项-hpguard 进程终止
打印机后台进程 c:\windows\system32\spool printers,servers, tool等, 清空printers中的内容就可以正常打印; 锐爽的后视镜跟其他的车型的后视镜不一 ...
- win10+vscode部署java开发环境
目录 Java开发插件配置: 调试: 快捷键: 启动配置文件launch.json: 启动配置说明: Launch: Attach: User Setting: 遇到的问题: 参考: Java开发插件 ...
- SSM到Spring Boot从零开发校园商铺平台
项目目的 特别 由于准备春招,所以希望各位看客方便的话,能去github上面帮我Star一下项目 https://github.com/Draymonders/Campus-Shop emmm, 已经 ...
- C++中的string常用函数用法
标准c++中string类函数介绍 注意不是CString 之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必 担心内存是否足够.字符串长度等等,而 ...
- html 之 table cellpadding,cellspacing
单元格(cell) -- 表格的内容 单元格边距(表格填充)(cellpadding) -- 代表单元格外面的一个距离,用于隔开单元格与单元格空间 单元格间距(表格间距)(cellspacing) - ...
- AtCoder Beginner Contest 117 解题报告
果然abc都是手速场. 倒序开的qwq. D题因为忘记1e12二进制几位上界爆了一发. A - Entrance Examination 就是除一下就行了... 看样例猜题意系列. #include& ...
- LuoguP2257 YY的GCD
题目描述 神犇YY虐完数论后给傻×kAc出了一题 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对 kAc这种傻×必然不会了,于 ...
- Long类型转json时前端js丢失精度解决方案
一.问题背景 Java后端开发过程中,尤其是id字段,因数值太大,通过json形式传输到前端后,在js解析时,会丢失精度. 如果对精度丢失没有什么概念,可以看一个知乎的帖子,来感受一下:https:/ ...