简体   繁体   English

如何声明一个全局变量来控制登录 Angular/Typescript?

[英]How to declare a global variable to control login Angular/ Typescript?

In the Angular/Typescript how I declare a global variable to control login status.在 Angular/Typescript 中,我如何声明一个全局变量来控制登录状态。 When I logged in, the global variable going to the true state, and when I log out how that changes to a false state.当我登录时,全局变量会变成真正的 state,而当我注销时,它会变成错误的 state。

I don't want it to sound as a snide question, but what do you mean when I log in ?我不希望它听起来像一个讽刺的问题,但是当我登录时你是什么意思? Since login happens against some server-based authentication provider, there is no built-in definition of "login" in Angular.由于登录是针对某些基于服务器的身份验证提供程序进行的,因此 Angular 中没有内置的“登录”定义。

Let's say you get a JWT token from the server when you supply login and password.假设您在提供登录名和密码时从服务器获得了JWT 令牌 That allows you to submit this token in subsequent requests and not get asked ("challenged") for login/password again.这允许您在后续请求中提交此令牌,而不会再次被询问(“挑战”)登录/密码。

In this case JWT has a number of claims (for example, exp that stands for expiration ) that gives a natural "global" variable to check whether user is logged in - and also whether their login hasn't timed out yet.在这种情况下,JWT 有许多声明(例如,代表过期exp ),它们提供了一个自然的“全局”变量来检查用户是否已登录 - 以及他们的登录是否尚未超时。

There are other ways to authenticate.还有其他方法可以进行身份验证。 Legacy was is cookie.旧版是 cookie。 So, you can read the cookie, and maybe even get some meaning out of it.因此,您可以阅读 cookie,甚至可以从中获得一些意义。 But the presence of the cookie doesn't automatically mean that the user is logged in, until the cookie is processed on the server.但是 cookie 的存在并不自动意味着用户已登录,直到 cookie 在服务器上得到处理。

So, in summary - JWT provides you both better authentication mechanism and easier way to verify that the user is logged in因此,总而言之 - JWT 为您提供了更好的身份验证机制和更简单的方法来验证用户是否已登录

Happy coding快乐编码

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

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