2

I need to configure my providers dynamically.

$config = [
    'client_id' = 'xxxxxxx',
    'client_token' = 'xxxxxxx',
    'redirect' = 'http://example.com/'
];
return Socialite::with($provider)->setConfig($config)->redirect();

But unfortunately there is no function setConfig.

I need to set provider, client_id, client_secret and redirect dynamically

Is there any ideas?

Thank you!

Mykyta Popov
  • 57
  • 1
  • 8
  • Also please consider https://stackoverflow.com/questions/53399021/socialite-google-redirect-uri-mismatch while changing url dynamically. – Amar Dec 28 '18 at 00:17

3 Answers3

7

You could use the Socialite buildProvider method like:

$config = [
    'client_id'    = 'xxxxxxx',
    'client_token' = 'xxxxxxx',
    'redirect'     = 'http://example.com/'
];

return Socialite::buildProvider(\Laravel\Socialite\Two\FacebookProvider::class, $config);

Where \Laravel\Socialite\Two\FacebookProvider::class would be swapped with your service (if different) as provided in either folder One/Two in https://github.com/laravel/socialite/tree/2.0/src

Ryan
  • 1,151
  • 5
  • 7
  • And one more, I do not understand how can I change \Laravel\Socialite\Two\FacebookProvider::class services, for example if I have included additional instagram provider... Could you explain? – Mykyta Popov Oct 12 '16 at 14:59
  • You can use the following package for the Instagram provider :) http://socialiteproviders.github.io/providers/instagram/ – Ryan Oct 12 '16 at 15:02
  • I see, it works. And I got new problem, when I set \Laravel\Socialite\One\TwitterProvider::class – Mykyta Popov Oct 12 '16 at 20:41
  • sorry I see, it works. And I got new problem, when I set \Laravel\Socialite\One\TwitterProvider::class - it returns the error "Type error: Argument 2 passed to Laravel\Socialite\One\AbstractProvider::__construct() must be an instance of League\OAuth1\Client\Server\Server, string given, called in /home/vagrant/Code/my-project/vendor/laravel/socialite/src/SocialiteManager.php on line 92" And \Laravel\Socialite\One\BitbucketProvider::class the same error – Mykyta Popov Oct 12 '16 at 20:44
  • this is not working when call callback ,it gives unauthrized 401 error. – Balaji Mar 06 '21 at 13:42
0

I use the following service provider in order to automatically fill in the redirect for each provider where it's empty.

It could be modified to update your configuration on the fly. It depends exactly what you're trying to do I suppose.

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class SocialServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        collect(config('services'))
            ->only(config('social.providers'))
            ->reject(function($config) {
                return array_get($config, 'redirect', false);
            })
            ->each(function($config, $key) {
                $url = url("login/{$key}/callback", [], true);

                config(["services.{$key}.redirect" => $url]);
            });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
    }
}
Ben Swinburne
  • 25,669
  • 10
  • 69
  • 108
0

This could help if anyone still faces the problem you can set the Redirect Url manually

    $driver = Socialite::driver('google');
    $driver->redirectUrl('your-custom-url');