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。