简体   繁体   中英

Lumen 5.5 Socialite Providers doesn't works with setConfig()

I use Laravel Socialite Providers ( https://socialiteproviders.github.io/ ) to login user on Lumen 5.5 API. setConfig() method, to force config, doesn't works for me...

Here below, my error and my code. The problem is that I do not know why I have this error.

Display Error:

Type error: Argument 1 passed to Laravel\\Socialite\\SocialiteManager::formatConfig() must be of the type array, null given, called in /home/vagrant/www/project1/api.website.app/vendor/laravel/socialite/src/SocialiteManager.php on line 125

PHP code:

$clientId = env('TWITTER_KEY');
$clientSecret = env('TWITTER_SECRET');
$redirectUrl = env('TWITTER_REDIRECT_URI');
$additionalProviderConfig = [];
$config = new SocialiteConfig($clientId, $clientSecret, $redirectUrl, $additionalProviderConfig);

return Socialite::with('twitter')->stateless()->setConfig($config)->redirect();

You need to configure services configuration first! Create a services.php file inside config folder (you may create this one if you don't have it already).

File services.php

return [
    'twitter' => [
        'client_id' => env('TWITTER_KEY'),
        'client_secret' => env('TWITTER_SECRET'),
        'redirect' => env('TWITTER_REDIRECT_URI'),
    ]
];

Your code should be like this:

use Laravel\Socialite\Facades\Socialite;

// You may not this one, read below explanation
app()->configure('services');

return Socialite::with('twitter')->stateless()->redirect();

It is better if you move the configure line to bootstrap/app.php file:

// Just right before register SocialiteProvider
$app->configure('services');
$app->register(SocialiteProviders\Manager\ServiceProvider::class);

If you have moved this configure , your code now should be:

use Laravel\Socialite\Facades\Socialite;

return Socialite::with('twitter')->stateless()->redirect();

PS:

If you have call to undefined stateless method, it means you don't set the listener yet, you can read here . Open your App\\Providers\\EventServiceProvider , add this line:

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        'App\Events\SomeEvent' => [
            'App\Listeners\EventListener',
        ],

        'SocialiteProviders\Manager\SocialiteWasCalled' => [
            'SocialiteProviders\Twitter\TwitterExtendSocialite@handle',
        ]
    ];
}

And don't forget to add this line to your bootstrap/app.php file:

$app->register(App\Providers\EventServiceProvider::class);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM