我是个新人,最近给公司做一个webapp(是那种扫描二维码进入页面的),测试的时候有个问题就是,如果关闭了页面,重新扫描二维码会要求重新登录,非常的麻烦,我查询了百度,发现用HTML5的localStorage可以解决,但我毕竟是个新人,所以不知道,这个如何和数据库里面的数据进行匹配或者校验?
ringa_lee2017-04-17 15:19:16
It depends on what kind of authentication your server uses. You have to ask the back-end personnel. If it is cookie authentication, there is no need to add special operations on the front-end.
If you want to authenticate the token, then each time you enter the page, call any interface that requires a token. If the call fails, it means you are not logged in. If the call is successful, it means you have logged in.
As long as you don’t clear the stored token or the token does not expire, you will be logged in automatically.
PHPz2017-04-17 15:19:16
islogin(){
let user = localStorage.getItem("USER_INFO")
if(user){
// 登陆了
}else{
// this.login({user})
}
},
login(){
....ajax
success(user){
// 登陆成功后
localStorage.setItem("USER_INFO",user)
}
}
ringa_lee2017-04-17 15:19:16
1. The front and back ends are not separated: the login status should be maintained by the back end;
2. The front and back ends are separated: after the user logs in successfully, the back end returns a token, and the front end stores the token locally (localstorage or cookie). Just bring the token with each request.