简体   繁体   English

如何在到达特定 URL 时销毁访问令牌?

[英]How to destroy access token on reaching a specific URL in react?

I have integrated another web app to mine.我已经集成了另一个网络应用程序到我的。 The user will login in my app, an access token will be generated and it will take him to the other app.用户将登录我的应用程序,将生成一个访问令牌并将他带到另一个应用程序。 Now the second app has a log out button in it which obviously does not destroy that access token but it takes users to a page having same url for all.现在第二个应用程序中有一个注销按钮,它显然不会破坏该访问令牌,但它将用户带到一个具有相同 url 的页面。 So the solution I thought for it is that the code will read the browser url and when that specific url appears, it will call the logout() function (as shown below for example) and that logout() function will destroy the token.所以我想到的解决方案是代码将读取浏览器 url,当出现该特定 url 时,它将调用logout()函数(如下所示)并且该 logout() 函数将销毁令牌。

if (window.location.href === "url") {
  logout();
}

function logout () {
  //code to destroy token..
}

I'm using window.location.href to fetch url from the browser.我正在使用window.location.href从浏览器中获取 url。 I have tested logout() function and it works fine.我已经测试了logout()函数,它工作正常。

Only thing I need is how to properly read the browser url and call the logout() function when a specific url is reached.我唯一需要的是如何正确读取浏览器 url 并在到达特定 url 时调用 logout() 函数。

On Logout from the Client Side, the easiest way is to remove the token from the storage of browser.在从客户端注销时,最简单的方法是从浏览器的存储中删除令牌。

But, What if you want to destroy the token on the Node server -但是,如果你想销毁节点服务器上的令牌怎么办 -

The problem with JWT package is that it doesn't provide any method or way to destroy the token. JWT 包的问题在于它没有提供任何方法或方式来销毁令牌。

So in order to destroy the token on the serverside you may use jwt-redis package instead of JWT因此,为了销毁服务器端的令牌,您可以使用 jwt-redis 包而不是 JWT

This library (jwt-redis) completely repeats the entire functionality of the library jsonwebtoken, with one important addition.这个库 (jwt-redis) 完全重复了库 jsonwebtoken 的全部功能,并增加了一个重要的功能。 Jwt-redis allows you to store the token label in redis to verify validity. jwt-redis 允许您将令牌标签存储在 redis 中以验证有效性。 The absence of a token label in redis makes the token not valid. redis中缺少token标签,导致token无效。 To destroy the token in jwt-redis, there is a destroy method在jwt-redis中销毁token,有destroy方法

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

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