Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help on @fastify/oauth2: Invalid State #1028

Open
fberrez opened this issue May 22, 2024 · 0 comments
Open

Help on @fastify/oauth2: Invalid State #1028

fberrez opened this issue May 22, 2024 · 0 comments
Labels
help wanted Extra attention is needed

Comments

@fberrez
Copy link

fberrez commented May 22, 2024

TL;DR

My authentication on google works when I go on my backend http://localhost:3000/login/google. It does not work through my frontend that performs this redirection: http://localhost:5173/login/google -> http://localhost:3000/login/google.


My backend is running on :3000. Here is my configuration for oauth2 google:

import Fastify from 'fastify';
import oauthPlugin from '@fastify/oauth2';

const fastify = Fastify();

fastify.register(oauthPlugin, {
  name: 'googleOAuth2',
  scope: ['email'],
  credentials: {
    client: {
      id: config.get('authentication.google.clientId'),
      secret: config.get('authentication.google.clientSecret'),
    },
    auth: oauthPlugin.GOOGLE_CONFIGURATION,
  },
  startRedirectPath: '/login/google',
  callbackUri: 'http://localhost:3000/login/google/callback',
});

Here is my callback controller:

static async callback(req, reply, instance) {
    instance.googleOAuth2.getAccessTokenFromAuthorizationCodeFlow(
      req,
      async (err, result) => {
        if (err) {
          reply.send(err);
          return;
        }
       ...
      });
}

When I open my browser and I go on http://localhost:3000/login/google, everything works fine: my callback controller has err = null.

So now, I have this react project with this simple component:

// http://localhost:5173/login/google
import { Button } from '../ui/button';

export function LoginButtonGoogle() {
  const handleLogin = () => {
    window.location.href = `http://localhost:3000/login/google`;
  };

  return <Button onClick={handleLogin}>Login with Google</Button>;
}

I'm redirected to the google authentication page. But when it goes back to my callback controller, err = Invalid State.

I don't understand why because it is juste a redirection performed on my frontend page without adding any other parameter.

Do you have any idea?

@fberrez fberrez added the help wanted Extra attention is needed label May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant