'Google Strategy NestJS

I have this issue, I think I've explored all the internet for it, I'm trying to implement an authentification with passport-google-oauth20 in NestJS.

I got an event click from the front then it goes in the first part of the code

  @UseGuards(GoogleAuthGuard)
  public async loginGoogle(@Req() req: any) {
  }

thens it goes ine the GoogleAuthGuard Part :

  constructor() {
   
    super({
      clientID: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_SECRET,
      callbackURL: 'http://localhost:5000/redirect',

      scope: ['email', 'profile'],
    });
    // super({ usernameField: 'token' });
  }

But in the network console part of the browser I got a CORS error 402 after a redirect 302.

Request URL: http://localhost:3000/redirect
Request Method: GET
Status Code: 302 Found
Remote Address: 127.0.0.1:3000
Referrer Policy: strict-origin-when-cross-origin

Request URL: https://accounts.google.com/o/oauth2/v2/auth?response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A5000%2Fredirect&scope=email%20profile&client_id=[MyID].apps.googleusercontent.com
Request Method: OPTIONS
Status Code: 405 
Remote Address: 216.58.204.109:443
Referrer Policy: strict-origin-when-cross-origin

I already tried to enable all type of cors :

  app.enableCors({
    origin: ['http://localhost:5000', 'http://localhost:3000'],
    allowedHeaders: ['content-type'],
    credentials: true,
    preflightContinue: false,
    optionsSuccessStatus: 204,
    methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
    // exposedHeaders: '*',
  });

I already configured the redirection on googleapis to allow this one too. I'm out of solution if anyone could help on it, it would be really appreciated.

It feels like the Authentification is good but when it's time to redirect something goes wrong and I can't put headers in the redirect.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source