Vue项目中实现用户登陆及token验证前端
先说一下个人实现步骤:vue
使用easy-mock模拟用户数据ios
我用的是easy-mock,新建了一个接口,用于模拟用户数据:vue-router
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
{
"error_code"
: 0,
"data"
: [{
"id"
:
'1'
,
"usertitle"
:
"管理员"
,
"username"
:
"admin"
,
"password"
:
"123456"
,
"token"
:
"@date(T)"
,
},
{
"id"
:
'2'
,
"usertitle"
:
"超级管理员"
,
"username"
:
"root"
,
"password"
:
"root"
,
"token"
:
"@date(T)"
,
}
]
}
|
login.vue中写好登录框:数据库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<template>
<div>
<p>用户名:<input type=
'text'
v-model=
"userName"
></p>
<p>密码:<input type=
'text'
v-model=
"passWord"
></p>
<button @click=
"login()"
>登陆</button>
</div>
</template>
<script>
export
default
{
data() {
return
{
userName:
'root'
,
passWord:
'root'
}
}
}
</script>
|
而后下载axios:npm install axios --save,用来请求刚刚定义好的easy-mock接口:npm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
login(){
const self =
this
;
var
res =response.data.data,
len = res.length,
userNameArr= [],
passWordArr= [],
ses= window.sessionStorage;
// 拿到全部的username
for
(
var
i=0; i<len; i++){
userNameArr.push(res[i].username);
passWordArr.push(res[i].password);
}
console.log(userNameArr, passWordArr);
if
(userNameArr.indexOf(
this
.userName) === -1){
alert(
'帐号不存在!'
);
}
else
{
var
index = userNameArr.indexOf(
this
.userName);
if
(passWordArr[index] ===
this
.passWord){
// 把token放在sessionStorage中
ses.setItem(
'data'
, res[index].token);
this
.$parent.$data.userTitle = res[index].usertitle;
//验证成功进入首页
this
.startHacking (
'登陆成功!'
);
//跳转到首页
this
.$router.push(
'/index'
);
// console.log(this.$router);
}
else
{
alert(
'密码错误!'
)
}
}
}).
catch
(err=>{
console.log(
'链接数据库失败!'
)
})
}
|
这一步最重要的是当帐号密码正确时,把请求回来的token放在sessionStorage中,axios
配置路由浏览器
而后配置路由新加一个meta属性:session
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
{
path:
'/'
,
name:
'login'
,
component: login,
meta:{
needLogin:
false
}
},
{
path:
'/index'
,
name:
'index'
,
component: index,
meta:{
needLogin:
true
}
}
|
判断每次路由跳转的连接是否须要登陆,ide
导航卫士
在main.js中配置一个全局前置钩子函数:router.beforeEach(),他的做用就是在每次路由切换的时候调用
这个钩子方法会接收三个参数:to、from、next。
用sessionStorage存储用户token
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
//路由守卫
router.beforeEach((to, from, next)=>{
//路由中设置的needLogin字段就在to当中
if
(window.sessionStorage.data){
console.log(window.sessionStorage);
// console.log(to.path) //每次跳转的路径
if
(to.path ===
'/'
){
//登陆状态下 访问login.vue页面 会跳到index.vue
next({path:
'/index'
});
}
else
{
next();
}
}
else
{
// 若是没有session ,访问任何页面。都会进入到 登陆页
if
(to.path ===
'/'
) {
// 若是是登陆页面的话,直接next() -->解决注销后的循环执行bug
next();
}
else
{
// 不然 跳转到登陆页面
next({ path:
'/'
});
}
}
})
|
这里用了router.beforeEach vue-router导航守卫
每次跳转时都会判断sessionStorage中是否有token值,若是有则能正常跳转,若是没有那么就返回登陆页面。
注销
至此就完成了一个简单的登陆状态了,浏览器关闭后sessionStorage会清空的,因此当用户关闭浏览器再打开是须要从新登陆的
固然也能够手动清除sessionStorage,清除动做能够作成注销登陆,这个就简单了。
1
2
3
4
5
6
|
loginOut(){
// 注销后 清除session信息 ,并返回登陆页
window.sessionStorage.removeItem(
'data'
);
this
.common.startHacking(
this
,
'success'
,
'注销成功!'
);
this
.$router.push(
'/index'
);
}
|
写一个清除sessionStorag的方法。
一个简单的保存登陆状态的小Demo。