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



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

-或-

如何从服务器收到令牌(由于成功登录(后,重定向到www.mypage.com/welcome页面,但只有当我表明自己已验证时,服务器才会将此页面发送给我。

导入此import { tokenNotExpired } from 'angular2-jwt'并添加此简单函数loggedIn() { return tokenNotExpired },rxjs 的某些功能在 angular 6 中被弃用,所以我使用 rxjs 兼容来实现向后兼容性。它解决了问题。 另外,请检查此 https://github.com/auth0/angular2-jwt

我假设你只是在这里使用香草JS。

可以使用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可用于将用户发送到 else 块内的另一个 URL,前提是他们的项目位于其本地存储中。

不,您不能通过本地存储执行此操作。这就是饼干存在的原因。

最新更新