I am using flask for a simple api which logins registers and update some data(notes) I am also using JWT for keeping track of logined users and the data is sent in json format
from flask import Flask, request, jsonify
from flask_cors import CORS, cross_origin
import jwt
app = Flask(__name__)
cors = CORS(
app,
supports_credentials=True,
)
@app.after_request
def after_request(response):
response.headers.add("Access-Control-Allow-Origin", "*")
return response
@app.route("/register", methods=["POST"])
@cross_origin()
def register():
name = request.get_json()["name"]
password = request.get_json()["password"]
existing_user = mycol.find_one({"name": name})
if existing_user:
print("User already exists. Please login.")
return jsonify({"message": "User already exists. Please login."}), 400
hashed_password = generate_password_hash(password)
user_id = str(uuid.uuid4())
new_user = {
"name": name,
"password": hashed_password,
"user_id": user_id,
"notes": [],
}
mycol.insert_one(new_user)
return jsonify({"message": "User registered successfully."}), 201
I feel this is the relevant part of my code. I am using this api in a react js website that throws error
Access to fetch at 'http://127.0.0.1:5000/' from origin 'http://localhost:5173' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header contains multiple values 'http://localhost:5173, POST', but only one is allowed. Have the server send the header with a valid value, or, if an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.