在 angularJS 中处理访问令牌

Handle access token in angularJS

我有网络 Api 和 AngularJS 来使用网络 Api。

当用户使用凭据登录时,用户会获得 access_token 并使用已颁发和过期字段刷新令牌。

access_token每1分钟过期一次,分配新token给用户。

现在的问题是

The time between token expired and allocation of new token to user.

if user do a page refresh then its makes an api call to load data of that page, but the access token was just expired and user does not got a new token, so old token is set in header of api call, hence user got 401 unauthorized as response and application throw user to log out.

我是第一次使用令牌,所以没有太多关于 access_token 和 refresh_token

的信息

所以,我不知道如何处理这种情况。

建议很重要。

每当您进行 API 调用以在页面上加载数据时,您应该在 callback 中检查 status code。如果 status code401,获取 refresh token 然后再次调用相同的 API 然后只初始化应用程序。否则使用 old response 值初始化应用程序。

感谢@Kushal 和@sahil 提供想法。

当用户的令牌过期时,应用将他们重定向到登录页面,因此在登录页面上添加了 api 调用以通过用户的 refresh_token 获取令牌,如果用户具有正确的刷新令牌,则将它们分配给新令牌并重定向到用户通过跟踪/维护用户当前页面的日志刷新的页面。及其工作原理。

再次感谢。