首页 > 解决方案 > 重新加载时删除的 Cookie - React 和 Flask

问题描述

浏览器重新加载时如何防止chrome删除cookie?

当用户创建如下所示的帐户时创建的 cookie,Expires/Max-age 为 1 个月后。但是,当我重新加载/刷新页面时,cookie 消失了。但是,post-man 中的 cookie 并没有被删除。 域(前端): http://localhost:3000/

域(后端):http: //127.0.0.1 :5000/

在此处输入图像描述

更新(2021 年 9 月 22 日): 在此处输入图像描述

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)

标签: pythonreactjsflaskcookies

解决方案


你们都做对了。我只是怀疑您的 TOKEN_EXPIRY_DATE 它必须是日期时间对象。

请参阅文档

检查您的过期值。

如果还是有问题。

请改用烧瓶登录。它将存储您的用户身份验证。并管理您的所有登录信息,使用起来非常简单。


推荐阅读