Home > Blockchain >  Cors error in nestjs. it is not working withCredentials
Cors error in nestjs. it is not working withCredentials

Time:11-20

I am getting below error while using "{withCredentials:true}" in the API .if I am not using this then it is not working.

const submit = async(e:SyntheticEvent) =>{
    e.preventDefault();
    await axios.post('http://localhost:8000/api/login',{
        email,
        password
    });
}

the above is working properly. When I am using {withCredentials:true} then it is giving cors() error.

const submit = async(e:SyntheticEvent) =>{
    e.preventDefault();
    await axios.post('http://localhost:8000/api/login',{
        email,
        password
    },{withCredentials:true}); 
    setRedirect(true);

enter image description here

}

in main.ts file I have below configurations.

  const options = {
    origin: '*',
    methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
    preflightContinue: false,
    credentials: true,
  };
  app.enableCors(options);

even I tried credentials: false, still it is not working.

I have to use {withCredentials:true} so that I can take jwt tokens for authorizations.

CodePudding user response:

try this.

  const options = {
    origin: true,
    methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
    preflightContinue: false,
    credentials: true,
    allowedHeaders: 'Content-Type, Accept',
  };
  app.enableCors(options);
  • Related