Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bilal_ch93's avatar

Dynamic redirectUrl in socialite when using multi auth

  • Socialite Version: ^4.3
  • Laravel Version: 6
  • PHP Version: 7.3.8
  • Database Driver & Version:MySQL

Description:

I'm using multiple auth guards. For that I have multiple callbacks for each guard. But redirectUrl doesn't seem to update the redirectUrl and keep throwing redirect_uri mismatch on facebook and google.

Steps To Reproduce:

  • clean install laravel
  • setup customer and worker guard.(not using users table for this)
  • setup routes as follows
    //social account worker sign up/sign in related routes
    Route::get('worker/{provider}', 'Auth\AuthController@redirectToProvider')->middleware('web');
    Route::get('worker/{provider}/callback', 'Auth\AuthController@handleProviderCallback')->name('worker.provider')->middleware('web');

for customer

    Route::get('customer/{provider}', 'Auth\AuthController@redirectToProvider')->middleware('web');
    Route::get('customer/{provider}/callback', 'Auth\AuthController@handleProviderCallback')->name('customer.provider')->middleware('web');

-setup provider id, key and default url to customer as follows FACEBOOK_URL=http://localhost:8000/api/customer/facebook/callback

  • add providers configs in config/services
  • in redirectToProvider method of customer
$redirectUrl = route('customer.provider', ['provider' => $provider]);
            return Socialite::driver($provider)
                ->redirectUrl($redirectUrl)
                ->redirect();

At this point, everything works fine Now if I run the redirectToProvider method for worker which is as follows

$redirectUrl = route('worker.provider',['provider' => $provider]);
return Socialite::driver($provider)->redirectUrl($redirectUrl)->redirect();

It throws "Client error: `POST https://graph.facebook.com/v3.3/oauth/access_token` resulted in a `400 Bad Request` response:\n{\"error\":{\"message\":\"Error validating verification code. Please make sure your redirect_uri is identical to the one you (truncated...)\n" for facebook and almost same error for google. I have tried to override env file at runtime and sending redirectUrl using "with"

0 likes
1 reply
Stephano's avatar

I am not sure if this is the best way, but the issue can be solved by override the config from the controller. Have to override in redirect and handler function as well.

eg:

    function googleLogin()
   {

            config(['services.google.redirect' => env('YOUR_ENV_REDIRECT_URL')]);
            return Socialite::driver('google')->redirect();

    }

    function googleLoginHandler()
    {

           config(['services.google.redirect' => env('YOUR_ENV_REDIRECT_URL')]);
           $user= Socialite::driver('google')->user();

           ...
           // your code to handle login

    }

   function googleSignUp()
   {
           return Socialite::driver('google')->redirect();
   }

   function googleSignUpHandler()
   {
          $user= Socialite::driver('google')->user();
          ...
          // your code to handle create user
   }

Please or to participate in this conversation.