Please Note: there are many questions like this one but none of them have worked for me which is why I am asking this.
My REST API is a Flask application. My /login endpoint uses JWTs and sets the access and refresh tokens in the client's cookies.
I am running Flask on 127.0.0.1:5000 and the frontend on 127.0.0.1:8080.
Backend Code
@auth.api(
http_path='/login',
http_method='POST',
)
def login(email, password):
# ...
access_token = create_access_token(identity=user.id, fresh=True, expires_delta=app.config['JWT_ACCESS_TOKEN_EXP'])
refresh_token = create_refresh_token(identity=user.id, expires_delta=app.config['JWT_REFRESH_TOKEN_EXP'])
resp = jsonify({'login': True})
set_access_cookies(resp, access_token)
set_refresh_cookies(resp, refresh_token)
# ...
return resp
My app has CORS enabled:
from flask_cors import CORS
# ...
cors = CORS()
def create_app():
app = Flask(__name__)
# ...
cors.init_app(app)
# ...
return app
Client Code
$.ajax({
method: "POST",
url: "http://127.0.0.1:5000/login",
data: JSON.stringify({email: 'myemail', password: 'mypassword'}),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (data) {
console.log('logged in!');
}
});
The Problem
The cookies are not being set in Firefox, Chrome nor Edge. However I can see the cookies in the Response Headers when using the browser's dev tools, but nothing appears in the Storage section, under Cookies.
I have tried multiple things:
- setting
cors=CORS(supports_credentials=True)on the backend and settingxhrFields:{ withCredentials: true}on the frontend. - setting
crossDomain: trueon the frontend - making sure no cookies are being blocked by the browser's settings
- running specifically on
127.0.0.1instead oflocalhost
Do I need some sort of proxy for the frontend? There must be an easier solution.
