'Nest.js is giving cors error even when cors is enabled
I am developing a next.js application with nest.js as the backend. Now, I am having cors error even when I have cors enabled in my main.ts
file of nest.js.
Here's my main.ts file.
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import cookieParser from 'cookie-parser';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
if (process.env.APP_ENV !== 'production') {
app.enableCors({
allowedHeaders: '*',
origin: '*',
credentials: true,
});
} else {
app.enableCors({
origin: process.env.FE_URL,
credentials: true,
});
}
app.use(cookieParser());
await app.listen(process.env.PORT || 5000);
}
bootstrap();
I also tried the following.
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import cookieParser from 'cookie-parser';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.enableCors({
allowedHeaders: '*',
origin: '*',
credentials: true,
});
app.use(cookieParser());
await app.listen(process.env.PORT || 5000);
}
bootstrap();
I also tried this
app.enableCors({
origin: 'http://localhost:3000',
credentials: true,
});
Now, from the frontend in _app.js
, I am defining Axios global config like the following.
axios.defaults.baseURL = 'http://localhost:5000';
axios.defaults.withCredentials = true;
Then in my login.tsx
file, I am sending the request to the nest.js application like the following.
const {data } = await axios.post('/auth/login', values);
Here's values is an object that has a username and password.
Here is the error.
I also tried every other solution from other StackOverflow questions. But none of them solved my problem. It actually worked a few days ago. I don't know what happened.
What am I doing wrong here? It's been driving me bananas now. If you need, I can provide more code.
Solution 1:[1]
As explained on the MDN Web Docs about CORS, you cannot use the wildcard (*
), whether it be to allow an origin or request headers (or request methods), in conjunction with credentialed requests. A more authoritative, but perhaps more dry, source is the Fetch standard:
For
Access-Control-Expose-Headers
,Access-Control-Allow-Methods
, andAccess-Control-Allow-Headers
response headers, the value*
counts as a wildcard for requests without credentials. For such requests there is no way to solely match a header name or method that is*
.
Accordingly, instead of
app.enableCors({
allowedHeaders: '*',
origin: '*',
credentials: true,
});
you should eschew the wildcard altogether and explicitly specify the allowed origin and allowed request headers, like so:
app.enableCors({
allowedHeaders: ['content-type'],
origin: 'http://localhost:3000',
credentials: true,
});
Solution 2:[2]
In my case I followed the official docs which says following to enable CORS on nestjs
const app = await NestFactory.create(AppModule);
app.enableCors();
await app.listen(3000);
but I still got the CORS error because of a CORS plugin which I installed long time ago.
So I switched off "Allow CORS" plugin in my browser and the error is gone. (If you have any similar plugin turn it off and see if it's working for you.)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | jub0bs |
Solution 2 | cigien |