Skip to content

CORS error when calling /api/v3/users with Authorization header in local setup

Technical Support
2 2 2
  • Hi NodeBB team,

    I have NodeBB running locally on my machine:

    NodeBB version: v3.12.7

    Environment: Local development

    Frontend: React (Vite) running on http://localhost:5173

    Backend (NodeBB) running on http://localhost:4567

    I’m trying to create a user via the API:

    async function registerUser() {
      try {
        const res = await fetch(`${import.meta.env.VITE_API_URL}v3/users`, {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
            "Authorization": `Bearer ${import.meta.env.VITE_TOKEN}`
          },
          body: JSON.stringify(formData),
        });
    
        if (!res.ok) {
          throw new Error(`HTTP error! Status: ${res.status}`);
        }
    
        const data = await res.json();
        console.log("User registered successfully:", data);
      } catch (error) {
        console.error("Error registering user:", error);
      }
    }
    

    Question:
    How can I correctly configure NodeBB in development so that it allows the Authorization header in API requests?
    Even after setting Access-Control-Allow-Headers in the ACP, the browser still fails at the preflight request.
    Do I need a plugin or middleware to handle CORS for API v3 routes?

  • Hi NodeBB team,

    I have NodeBB running locally on my machine:

    NodeBB version: v3.12.7

    Environment: Local development

    Frontend: React (Vite) running on http://localhost:5173

    Backend (NodeBB) running on http://localhost:4567

    I’m trying to create a user via the API:

    async function registerUser() {
      try {
        const res = await fetch(`${import.meta.env.VITE_API_URL}v3/users`, {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
            "Authorization": `Bearer ${import.meta.env.VITE_TOKEN}`
          },
          body: JSON.stringify(formData),
        });
    
        if (!res.ok) {
          throw new Error(`HTTP error! Status: ${res.status}`);
        }
    
        const data = await res.json();
        console.log("User registered successfully:", data);
      } catch (error) {
        console.error("Error registering user:", error);
      }
    }
    

    Question:
    How can I correctly configure NodeBB in development so that it allows the Authorization header in API requests?
    Even after setting Access-Control-Allow-Headers in the ACP, the browser still fails at the preflight request.
    Do I need a plugin or middleware to handle CORS for API v3 routes?

    balu can you confirm that the response you receive in the Vite app indeed contains the restrictive ACAO headers irrespective of what is set in the ACP?

Diese Artikel könnten Dich auch interessieren.

  • Email confirmation for all users

    Technical Support
    6
    0 Stimmen
    6 Beiträge
    3 Aufrufe
    julian@community.nodebb.orgJ
    eugene_thedev session sharing plugin will look for id first, and check it against the stored hash for it. Otherwise it will fall back to email.
  • Is there a way to show fixed categories in custom page?

    Technical Support
    2
    0 Stimmen
    2 Beiträge
    5 Aufrufe
    julian@community.nodebb.orgJ
    Yuting_11 that's an interesting use case... I don't think we have a widget that does that right now, but a widget to display a single (or multiple) categories could be interesting! If multiple, though, then you'd probably want to be able to order them, so that makes it difficult.
  • Cannot change user slug

    Technical Support
    26
    0 Stimmen
    26 Beiträge
    8 Aufrufe
    julian@community.nodebb.orgJ
    NodeHam I'm sorry, have been busy with other tasks and have not found the time to take a look yet.
  • 0 Stimmen
    2 Beiträge
    4 Aufrufe
    julian@community.nodebb.orgJ
    Sounds like you figured it out in the other topic
  • Static Images Randomly Disappear

    Technical Support
    6
    0 Stimmen
    6 Beiträge
    15 Aufrufe
    julian@community.nodebb.orgJ
    Onii what are you using as your reverse proxy?
  • Image saving

    Technical Support
    7
    0 Stimmen
    7 Beiträge
    19 Aufrufe
    julian@community.nodebb.orgJ
    You can use the S3 plugin. However, the underlying problem is that you are using some sort of service with an ephemeral virtual drive. It gets cleared every time the service restarts. If you don't want to use s3, you will have to set up a volume and mount it to the /public/uploads folder.
  • How push notification works?

    Technical Support
    11
    0 Stimmen
    11 Beiträge
    28 Aufrufe
    julian@community.nodebb.orgJ
    ska correct, since web-push relies on more modern browser supported technology and doesn't require a third-party service (in this case, ntfy.sh)
  • Upgrade form 3.x to 4.x

    Technical Support
    16
    0 Stimmen
    16 Beiträge
    26 Aufrufe
    julian@community.nodebb.orgJ
    baris awesome work as always!