Unable to use Laravel / Socialite with Lumen

I am trying to use the Laravel \ Socialite package on my system in Lumen (5.1)

I added this to the config\services.php file:

 <?php //Socialite 'facebook' => [ 'client_id' => '##################', 'client_secret' => '##################', 'redirect' => 'http://local.dev/admin/facebook/callback', ], 

In bootstrap\app.php file:

 class_alias(Laravel\Socialite\Facades\Socialite::class, 'Socialite'); $app->register(Laravel\Socialite\SocialiteServiceProvider::class); 

Then I created a controller for facebook authentication:

 <?php namespace App\Http\Controllers\Facebook; use App\Http\Controllers\Controller; use Laravel\Socialite\Contracts\Factory as Socialite; class FacebookController extends Controller { public function redirectToProviderAdmin() { return Socialite::driver('facebook')->scopes(['manage_pages', 'publish_actions'])->redirect(); } public function handleProviderCallbackAdmin() { $user = Socialite::driver('facebook')->user(); } } 

And in routes.php :

 $app->get('/admin/facebook/login', 'App\Http\Controllers\Facebook\ FacebookController@redirectToProviderAdmin '); $app->get('/admin/facebook/callback', 'App\Http\Controllers\Facebook\ FacebookController@handleProviderCallbackAdmin '); 

I just followed the documentation, changing according to my needs. When I go to the http://local.dev/admin/facebook/login page, I get the following error:

The non-static method Laravel \ Socialite \ Contracts \ Factory :: driver () cannot be called statically, assuming $ this from an incompatible context

Indeed, according to the code, the driver function must be instantiated.

EDIT . And if I try to create this class, I get the following error:

Unable to create Laravel \ Socialite \ Contracts \ Factory interface

How to make this module work?

+2
php facebook oauth laravel lumen
source share
2 answers

this is how it works in my case

in services.php file

  'facebook' => [ 'client_id' => '***************', 'client_secret' => '***************', 'redirect' => "" ], 

I left the redirect empty because my site is multilingual (therefore it is populated with sessions a bit later). if you use only one language, put your absolute callback path there. eg

 "http://example.com:8000/my-callback/"; 

also check the configuration of /app.php. in an array of suppliers

 Laravel\Socialite\SocialiteServiceProvider::class, 

in an array of aliases

 'Socialite' => Laravel\Socialite\Facades\Socialite::class, 

my routes are as follows

 Route::get('facebook', 'Auth\ AuthController@redirectToProvider '); Route::get('callback', 'Auth\ AuthController@handleProviderCallback '); 

here are the methods of auth controllers. put up

 use Socialite; //იობანი როტ public function redirectToProvider(Request $request) { return Socialite::with('facebook')->redirect(); } public function handleProviderCallback(Request $request) { //here you hadle input user data $user = Socialite::with('facebook')->user(); } 

my facebook app fb_settings

giga.com:8000 is my localhost (so its same localhost: 8000)

fb_settings2

fb_settings3

as you can see in the actual OAuth redirect URI, you should put your callback there. in my case, I use three URLs because I have three languages. in your case it should be just

 http://your-domain-name.com:8000/callback 

if you are working on localhost you should specify your domain in config / services.php mine looks like this

 'domain' => "your-domain.com", 

after running all these commands

 php artisan cache:clear php artisan view:clear composer dump-autoload 

restart the server, clear the browser cookies. hope this helps

0
source share

I found the cause of this error. Just don't write

 use Laravel\Socialite\Contracts\Factory as Socialite; 

But

 use Socialite; 

Then I fell back on concern. I still believe that my answer is only related to Lumen , because the documentation was done for Laravel.

They tell us to create the config\services.php file and enter the application passwords. Do not do this, because this file will never be called anywhere.

However, you must add the identifiers in the bootstrap\app.php , for example:

 config(['services' => [ 'facebook' => [ 'client_id' => '##################', 'client_secret' => '##################', 'redirect' => 'http://local.dev/admin/facebook/callback', ], ]]); 

Check: facebook method in Laravel\Socialite\SocialiteManager.php file:

 protected function createFacebookDriver() { dd($config = $this->app['config']['services']); return $this->buildProvider( 'Laravel\Socialite\Two\FacebookProvider', $config ); } 

This code call is app > config > services . Thus, the above configuration is correct. After the test, everything works!

-one
source share

All Articles