简体   繁体   English

如何在react js中实现刷新令牌

[英]How to implement refresh token in react js

The response from the server set the access and refresh token to the client(reactjs) as an httponly cookie.来自服务器的响应将访问和刷新令牌设置为客户端(reactjs)作为 httponly cookie。 How can I use the refresh token to generate another access token, because when I try to use document.cookie or cookie.get('refresh') it returned undefined.如何使用刷新令牌生成另一个访问令牌,因为当我尝试使用document.cookiecookie.get('refresh')它返回未定义。

axios.post('http://localhost:8000/api/token/refresh/', {
    refresh: cookie.get('refresh') // Cookie here is undefined
}).then(response => {
    // new access token
})

Well.好。 As its httponly cookie.作为它的 httponly cookie。 you dont have access to get it with javaScript.您无权使用 javaScript 获取它。 if the backend set it well.如果后端设置好。 it will save on your cookies by default and transfer between cliend and server on each httpRequest with no specific configuration .默认情况下,它会保存您的 cookie,并在每个 httpRequest 的客户端和服务器之间传输,无需特定配置 Also you can check httponly cookie on your browser console cookies.您也可以在浏览器控制台 cookie 上检查 httponly cookie。 (it should have httponly green tick icon) So what should you do next for generate another access token? (它应该有 httponly 绿色勾号图标)那么接下来你应该做什么来生成另一个访问令牌? you need to implement an interceptor.你需要实现一个拦截器。 how?如何? these documentations will help you.这些文件会对您有所帮助。 enter link description here 在此处输入链接描述

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

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