Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

234
Visualizações
Access control allow origin is always * when credentials is true

i am building a graphql api using apollo-server-express. I am using jwt and cookies for authentication so in my cors options credentials is set to true, the thing is, when credentials is set to true, Access-Control-Allow-Origin is always *, even if i set the origin in my cors options, it still is *.

here is my middleware:

app.use(
  cors({
    origin: "https://studio.apollographql.com",
    credentials: true,
  })
);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json({ limit: "2mb" }));
app.use(cookieParser());

To reproduce you need a graphql and express server.

// Initialize dotenv
dotenv.config();

// Get env variables
const PORT = process.env.PORT || 5000;

// Create express server
const app = express();

// Express server middleware
app.use(
  cors({
    credentials: true,
    origin: "https://studio.apollographql.com",
  })
);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json({ limit: "2mb" }));
app.use(cookieParser());

// Create new apollo server
const gqlServer = new ApolloServer({
  typeDefs: [typedefs],
  resolvers: { ...resolvers },
  context: ({ req, res }: ContextType) => {
    // const { isValid, userData } = verifyToken(req.cookies?.xToken);

    return {
      // isLoggedIn: isValid,
      // userData: userData,
      req,
      res,
    };
  },
});

(async () => {
  await gqlServer.start();

  gqlServer.applyMiddleware({ app });

  app.listen(PORT, () => {
    console.log(`Server listening on port ${PORT}`);
  });
})();

Then you need to go on localhost:5000/graphql, tap query your server, and go to apollo. In apollo studio, on the top left, where your url is displayed (should be localhost:5000), there should be a little cog icon (settings icon), you can tap on that, then go to include cookies and set it on. Thats it, if you go in dev tools at the network tab, you'll see cors errors from your server localhost:5000 ant the value of Access-Control-Allow-Origin is *. btw you need to set up some resolvers and type definitions.

Update

I solved the problem, i needed to apply the cors middleware to my graphql server as well.

  gqlServer.applyMiddleware({
    app,
    cors: {
      origin: "https://studio.apollographql.com",
      credentials: true,
    },
  });
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda