Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
We Distribute
  1. Home
  2. Technical Support
  3. CORS error when calling /api/v3/users with Authorization header in local setup

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

Scheduled Pinned Locked Moved Technical Support
2 Posts 2 Posters 0 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B This user is from outside of this forum
    B This user is from outside of this forum
    balu@community.nodebb.org
    wrote last edited by
    #1

    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?

    julian@community.nodebb.orgJ 1 Reply Last reply
    0
    • B balu@community.nodebb.org

      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?

      julian@community.nodebb.orgJ This user is from outside of this forum
      julian@community.nodebb.orgJ This user is from outside of this forum
      julian@community.nodebb.org
      wrote last edited by
      #2

      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?

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      Powered by NodeBB Contributors
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups