I am working on a project where I use flask as a backend and reactjs for the front-end. When I try to set a cookie from flask, the cookie is not set on the browser. I have been researching what could be wrong. I have an endpoint /login in flask where, when the user logs in from reactjs, a JWT is created and a cookie is set. My flask app is running on port 5000 and my react app is running on port 3000. During my research on this problem, I came across multiple solutions, but they still did not work, like setting CORS(app, support_credentials=True) and also including the domain keyword argument in set cookie.
Here is the code from my flask app:
# login route - this will set cookie (JWT token)
@app.route('/login', methods=['POST'])
@cross_origin()
def login():
auth = request.authorization
# the auth should have a username and a password
user = User.query.filter_by(username=auth['username']).first()
if not user:
return jsonify({'msg': 'user not found'}), 404
if bcrypt.check_password_hash(user.password, auth['password']):
# generate a token
access_token = create_access_token(identity=user.username, expires_delta=datetime.timedelta(days=1))
response = make_response({'msg': 'successfully logged in!'})
response.headers['Access-Control-Allow-Credentials'] = True
response.set_cookie('access_token', value=access_token, domain='127.0.0.1:3000')
# we need to convert the response object to json
return jsonify({'response': response.json}), 200
return jsonify({'msg': 'wrong credentials'}), 401