Search code examples
pythonflaskcorssetcookieflask-cors

Response Cookie not getting set in CORS flask


I am trying to set a cookie on a website a.com by making a fetch call from b.com

a.com is a flask application with below code

from flask import Flask, request, make_response
from flask_cors import CORS,logging
logging.getLogger('flask_cors').level = logging.DEBUG

app = Flask(__name__)
CORS(app,supports_credentials=True)

@app.route("/")
def helloWorld():
  return "Hello, cross-origin-world!"

@app.route('/setcookie', methods = ['GET'])
def setcookie():
  resp = make_response("Cookie Set")
  resp.set_cookie('userID', "test",max_age=60*60*24)
  return resp

@app.route('/getcookie')
def getcookie():
  name = request.cookies.get('userID')
  return '<h1>welcome '+name+'</h1>'

app.run(host="0.0.0.0", port=5000)

b.com is a simple web page with below code:

<html>
<head></head>
<body>
<script>
const url = "http://a.com:5000/";
fetch(`${url}setcookie`,{credentials: 'include'}).then(()=>{
    fetch(`${url}getcookie`,{credentials: 'include'});
})
</script>
Application page
</body>
</html>

When we load a.com it should ideally be able to get the cookie, this is working if they both are in the same domain.


Solution

  • Started working after enabling third-party cookies in google chrome.