简体   繁体   English

如果我在本地存储中存储了令牌,刷新页面后如何保持登录状态

[英]How to stay logged in after refreshing the page if I have token stored in local storage

How to stay logged in after refreshing the page if I have token stored in local storage. 如果我在本地存储中存储了令牌,刷新页面后如何保持登录状态。

-or- -要么-

How to after receiving token from server(due to successful login), redirect to www.mypage.com/welcome page, but server will send to me this page only if I identify yourself as verified. 从服务器接收令牌后如何进行操作(由于成功登录),请重定向到www.mypage.com/welcome页面,但是只有当我确认自己已通过验证时,服务器才会向我发送此页面。

import this import { tokenNotExpired } from 'angular2-jwt' and add this simple function loggedIn() { return tokenNotExpired } , some function of rxjs was deprecated in angular 6, so I use rxjs-compat for backward compatibility. import { tokenNotExpired } from 'angular2-jwt'导入此import { tokenNotExpired } from 'angular2-jwt'并添加此简单函数loggedIn() { return tokenNotExpired }loggedIn() { return tokenNotExpired }某些功能在angular 6中已弃用,因此我使用rxjs-compat来实现向后兼容性。 It solve the issue. 它解决了问题。 Also, check this https://github.com/auth0/angular2-jwt 另外,请检查此https://github.com/auth0/angular2-jwt

I'm assuming you're just using vanilla JS here. 我假设您只是在这里使用香草JS。

You can check if an item is in local storage by using the localStorage.getItem('yourItem') method and an if statement, which would look something like: 您可以使用localStorage.getItem('yourItem')方法和if语句检查某项是否在本地存储中,该语句类似于:

if (localStorage.getItem('yourItem') === null) {
  // code to execute if the item isn't in local storage
} else {
  // code to execute if the item is in local storage
  window.location = //full url to redirect to

window.location can be used to send the user to another URL inside the else block if they have your item in their local storage. window.location可用于将用户发送到else块内的另一个URL(如果他们的项目位于其本地存储中)。

No, you can't do it via local storage. 不,您不能通过本地存储来做到这一点。 That's why cookies exist. 这就是存在cookie的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM