Flask CORS + axios

Viewed 379

My frontend - http://localhost:8081

My backend - http://localhost:5000 (Flask)

I'm trying to make axios work with the withCredentials: true configuration, but I get the CORS error.

On the server I use:


cors = CORS(app, resources={r"/auth/*": {"origins": "*"}})

@app.route("/auth/login", methods=["POST"])
def login():
    payload = request.get_json()
    email = payload["email"]
    password = payload["password"]
    current_user = User.find_by_email(email)
    if not current_user:
        return {'message': 'User {} doesn\'t exist'.format(email)}
    if User.verify_hash (password, current_user.password):
        access_token = create_access_token(identity=email)
        refresh_token = create_refresh_token(identity=email)
        return jsonify({
            'message': 'Logged in as {}'.format(current_user.email),
            'access_token': access_token,
            'refresh_token': refresh_token
        })
    else:

        return jsonify({'message': 'Wrong credentials'}), 500 

but it doesn't help.

How do I make it work?

UPD for those who have encountered the same problem with Flask, here is the solution :

CORS_ALLOW_ORIGIN="*,*"
CORS_EXPOSE_HEADERS="*,*"
CORS_ALLOW_HEADERS="content-type,*"
cors = CORS(app, origins=CORS_ALLOW_ORIGIN.split(","), allow_headers=CORS_ALLOW_HEADERS.split(",") , expose_headers= CORS_EXPOSE_HEADERS.split(","),   supports_credentials = True)
0 Answers
Related