python - 重新加载时删除的 Cookie - React 和 Flask
问题描述
浏览器重新加载时如何防止chrome删除cookie?
当用户创建如下所示的帐户时创建的 cookie,Expires/Max-age 为 1 个月后。但是,当我重新加载/刷新页面时,cookie 消失了。但是,post-man 中的 cookie 并没有被删除。 域(前端): http://localhost:3000/
域(后端):http: //127.0.0.1 :5000/
React:
这就是我执行获取请求的方式。我将凭据设置为include
.
fetch(domain + "/account/create", {
method: 'POST', // or 'PUT'
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
credentials: 'include',
body: JSON.stringify(inputData),
})
.then(res => res.json())
.then(data => {
console.log('Success:', data);
return(data);
})
.catch((error) => {
setErrorMsg(error.message);
});
烧瓶(后端):
resp = Response(
response=json.dumps(
{ 'message': 'Account Created',
'token': f'{encoded_jwt}'}),
status=200,
mimetype="application/json"
)
resp.headers.add('Access-Control-Allow-Origin', 'http://localhost:3000')
resp.headers.add('Access-Control-Allow-Credentials', 'true')
resp.set_cookie('token', value=encoded_jwt, httponly= True, expires = TOKEN_EXPIRY_DATE, samesite='None', secure=True)
解决方案
你们都做对了。我只是怀疑您的 TOKEN_EXPIRY_DATE 它必须是日期时间对象。
检查您的过期值。
如果还是有问题。
请改用烧瓶登录。它将存储您的用户身份验证。并管理您的所有登录信息,使用起来非常简单。
推荐阅读
- typescript - 基于变量的条件类型
- python - Python Spark - 将时间戳转换为 YYYY-MM-DD
- react-native - 应用程序在后台时Websocket断开连接(REACT NATIVE)
- javascript - 将对象中的前 7 个键复制到新对象中
- node.js - Google oauth2 将请求从子域重定向到主域
- mysql - 分组表上的多个总和
- javascript - 反应错误:无法读取未定义的属性“上下文”
- python - 从python中的网站中提取句子中的一些文本
- xcode - 通过 Xcode 将命令行 arg 传递给 react-native run-ios
- r - 从R中的字符串中提取各种格式的日期