【问题标题】:Cookie being passed to client, but not being saved in browserCookie 被传递给客户端,但未保存在浏览器中
【发布时间】:2019-09-10 10:56:21
【问题描述】:

如下图所示,我正在网络中获取 cookie

但在浏览器中显示为空白

前端代码反应

  signIn: (username, password) =>
    axios
      .post('http://127.0.0.1:5000/login', {
        data: {
          Username: username,
          Password: password,
        },
      })
      .then(response => {
        if (response.status === 200) {
          console.log('sign in response', response);
          return response;
        }
      })
      .catch(error => {
        if (error.response && error.response.status === 400) {
          return error;
        }
        throw error;
      }),
};
axios.defaults.withCredentials = true;

后端代码

    username = request.json['data']['Username']
    password = request.json['data']['Password']

    try:
        u = Cognito(COGNITO_USER_POOL_ID, COGNITO_CLIENT_ID, username=username)
        u.authenticate(password=password)

        resp = make_success_resp({'username': u.username})
        # 31557600 sec in 1 year
        resp.set_cookie('ATOK',str(u.access_token),max_age=60*60*24*365*2, domain= 'app.localhost')
        resp.set_cookie('IDTOK', u.id_token)
        resp.set_cookie('RTOK', u.refresh_token)
        resp.headers["Access-Control-Allow-Credentials"]=True
        resp.headers["Access-Control-Allow-Origin"]="*"
        return resp
    except ClientError as e:
        print(username + ": ", e.response['Error']['Message'])
        return make_err_resp(e.response['Error']['Message'], 400)
from flask_cors import CORS
CORS(app,origin='http://localhost:3000',credentials='true')

【问题讨论】:

  • 请试一试Github Repository 中的用例 26,如果对您有帮助,请告诉我。

标签: reactjs cookies flask redux


【解决方案1】:

假设您的代码没有任何问题,我认为这可能是由于使用了 localhost。尝试将主机名映射到主机文件中的 localhost 并改用它:127.0.0.1 testsite.dev 并将 cookie 设置为 http://testsite.dev:port

【讨论】:

    猜你喜欢
    • 2015-10-31
    • 2017-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    • 2018-05-19
    • 2019-11-04
    • 1970-01-01
    相关资源
    最近更新 更多