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

How to use Google login on Symfony which is used on Apache2 Reverse Proxy? #430

Open
profesionalaiesam opened this issue Feb 5, 2024 · 2 comments

Comments

@profesionalaiesam
Copy link

I have a running container inside my server which rans on 1285 port.
I use Apache2 reverse proxy to serve that instance through 80 port on my specific domain and then apply SSL certificate there.

And I have this code as my Symfony authenticator

MyGoogleAuthenticator.php

    public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
    {
        $targetUrl = $this->router->generate(name: 'admin', referenceType: UrlGeneratorInterface::RELATIVE_PATH);
        /** @var User $user */
        $user = $token->getUser();
        $this->userLogService->logUserLogin($user, $request->getClientIp());

        return new RedirectResponse($targetUrl);
    }

    public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
    {
        $this->flashBag->add('danger', 'User is not registered');
        $targetUrl = $this->router->generate(name: 'app_login', referenceType: UrlGeneratorInterface::RELATIVE_PATH);

        return new RedirectResponse($targetUrl);
    }

What I do:

  1. I go to https://mydomain.com/connect/google
  2. I login with my account
  3. I get redirected to http://localhost:1285/connect/google/check

How I want it to work:

  1. I go to https://mydomain.com/connect/google
  2. I login with my account
  3. I get redirected to https://mydomain.com/connect/google/check

What is the correct or recommended way of achieving this?

@profesionalaiesam
Copy link
Author

My GoogleController.php looks like this

    /**
     * Link to this controller to start the "connect" process.
     *
     * @Route("/connect/google", name="connect_google_start")
     */
    public function connectAction(ClientRegistry $clientRegistry): RedirectResponse
    {
        if (!$this->settingsReadService->isGoogleLoginEnabled()) {
            throw new \Exception('Google login is not enabled');
        }

        return $clientRegistry
            ->getClient('google') // key used in config/packages/knpu_oauth2_client.yaml
            ->redirect([
                'openid', 'email', 'profile', // the scopes you want to access
            ], []);
    }

    /**
     * After going to Google, you're redirected back here
     * because this is the "redirect_route" you configured
     * in config/packages/knpu_oauth2_client.yaml.
     *
     * @Route("/connect/google/check", name="connect_google_check")
     */
    public function connectCheckAction(Request $request, ClientRegistry $clientRegistry)
    {
    }

@julbrs
Copy link

julbrs commented Feb 9, 2024

See this bug here, it's well documented: #315

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants