Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next.JS API Call to Flask API POST Endpoint - Access-Control-Allow-Credentials is not set properly #321

Open
mtdutaro opened this issue Jul 11, 2022 · 0 comments

Comments

@mtdutaro
Copy link

I'm trying to pass in a cookie to a Flask API POST Endpoint

export async function login(username, password, csrfToken, sessionCookie) {
  console.log(sessionCookie);
  const res = await fetch(buildLink("api/login/"), {
    method: "POST",
    credentials: "include",
    headers: {
      "Content-Type": "application/json",
      "X-CSRFToken": csrfToken,
    },
    body: {
      username: username,
      password: password,
      cookie: sessionCookie,
    },
  });
  const user = await res.json();
  console.log(user);
  return user;
}

However, the error I get when making this request in the browser is

Access to fetch at 'http://127.0.0.1:2476/api/login/' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'.

My set-up using flask-cors is

login_manager = LoginManager()
login_manager.init_app(app)
login_manager.session_protection = "strong"
csrf = CSRFProtect(app)
cors = CORS(
    app,
    resources={
        r"*": {
            "origins": [
                "http://localhost:8080",
                "http://localhost:3000",
                "http://127.0.0.1:3000",
                "http://127.0.0.1:8080",
            ]
        }
    },
    expose_headers=["Content-Type", "X-CSRFToken"],
    supports_credentials=True,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant