Google authentication using Ravel's Socialite does not redirect successfully when you select a Google account and log in.

Asked 2 years ago, Updated 2 years ago, 87 views

There seems to be no problem until authentication, but the redirect destination after authentication says, "I can't access this site."
There is no particular error indication.Please teach me.
Environment: With DockerToolbox.
Windows 10

Redirect URL in Google API
Enter a description of the image here
The URL set in the address field is written.
Enter a description of the image here
web.php

Route::prefix('login') ->name('login.') ->group(function(){
    Route::get('/{provider}', 'Auth\LoginController @redirectToProvider') ->name('{provider}');
    Route::get('/{provider}/callback', 'Auth\LoginController@handleProviderCallback') ->name('{provider}.callback');
});

Specifies routing.

LoginController.php

<?php

namespace App\Http\Controllers\Auth;

useApp\Http\Controllers\Controllers;
use App\User;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
// =========== Facade ==========
use Ravel\Socialite\Facades\Socialite;

class LoginController extensions Controller
{
    // approximately
    public function redirectToProvider (string$provider)
    {
        return Socialite::driver($provider)->redirect();
    }

    public function handleProviderCallback(Request$request, string$provider)
    {
        $providerUser=Socialite::driver($provider)->stateless()->user();

        $user=User::where('email', $providerUser->getEmail()) ->first();

        if($user){
            $this->guard()->login($user,true);
            return$this->sendLoginResponse($request);
        }
        
    }
}

login.blade.php

<h2class="h3card-title text-center mt-2">Login</h2>

   <a href="{{route('login.{provider}',['provider'=>'google']}}"class="btn btn-block btn-info">
       <i class="fab fa-Google mr-1">/i>Log in with Google
   </a>

laravel google-api

2022-09-30 17:56

1 Answers

I solved myself.
If you are using DokcerToolbox, you will need to do something called port forwarding.

After opening a command prompt with administrator privileges and executing the following commands, you are now redirected successfully:

netsh interface portproxy add v4 tov4 listenport=80 listenaddress=127.0.0.1 connectport=80 connectaddress=192.168.99.100
netsh interface portproxy show v4 tov4

Thank you.


2022-09-30 17:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.