简体   繁体   English

在 NextJS 中捕获服务器端 400 错误,然后删除一个 cookie

[英]Catch Server Side 400 error in NextJS and then delete a cookie

I have a server-side route /auth/refresh to refresh tokens.我有一个服务器端路由 /auth/refresh 来刷新令牌。 I send a Post from NextJS client side with the current token and the server searches for it.我使用当前令牌从 NextJS 客户端发送一个帖子,服务器搜索它。 If it does not find it then it sends a 400 Error: Refresh token not found .如果找不到它,则会发送 400 Error: Refresh token not found When this happens I want NextJS to automatically delete the tokens it has client side which will force the user to reauthenticate.发生这种情况时,我希望 NextJS 自动删除它在客户端的令牌,这将强制用户重新进行身份验证。 The documentation does not seem to handle this use case.文档似乎没有处理这个用例。 Do you all have any ideas?大家有什么想法吗?

It is pretty simple, when user token is expired you need to set again that cookie with a lifetime of 0/1 ms, then (and here depends a lot how do you handle you unauthorised users) reddirect the user to login/not authorised page.这很简单,当用户令牌过期时,您需要再次设置该 cookie 的生命周期为 0/1 毫秒,然后(这在很大程度上取决于您如何处理未经授权的用户)将用户重定向到登录/未授权页面. A little code would help to offer you more precise suggestion.一些代码将有助于为您提供更精确的建议。

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

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