Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
403 views
in Technique[技术] by (71.8m points)

php - Unable to use Laravel / Socialite with Lumen

I try to use the package LaravelSocialite in my system in Lumen (5.1)

I added this in the configservices.php file :

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

In bootstrapapp.php file :

class_alias(LaravelSocialiteFacadesSocialite::class, 'Socialite');
$app->register(LaravelSocialiteSocialiteServiceProvider::class);

Then I created a controller for the facebook authentication :

<?php

namespace AppHttpControllersFacebook;

use AppHttpControllersController;
use LaravelSocialiteContractsFactory 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 the routes.php :

$app->get('/admin/facebook/login', 'AppHttpControllersFacebookFacebookController@redirectToProviderAdmin');
$app->get('/admin/facebook/callback', 'AppHttpControllersFacebookFacebookController@handleProviderCallbackAdmin');

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

Non-static method LaravelSocialiteContractsFactory::driver() cannot be called statically, assuming $this from incompatible context

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

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

Cannot instantiate interface LaravelSocialiteContractsFactory

How do you make this module to work?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

here's how that works in my case

in services.php file

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

i left redirect empty cause my site is multilingual (so, it fills in a bit later with sessions). if you use only one language, put there your callback absolute path. for example

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

also check your config/app.php. in providers array

LaravelSocialiteSocialiteServiceProvider::class,

in aliases array

'Socialite' => LaravelSocialiteFacadesSocialite::class,

my routes look like this

Route::get('facebook', 'AuthAuthController@redirectToProvider');
Route::get('callback', 'AuthAuthController@handleProviderCallback');

here's auth controllers methods. put in top

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 the same localhost:8000)

fb_settings2

fb_settings3

as you can see in Valid OAuth redirect URI, you should put there your callback. in my case i use three urls cause i have three languages. in your case it should be just

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

if you work on localhost, you should name your domain in config/services.php mine look like this

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

after everything run these commands

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

restart your server, clear your browser cookies. hope it helps


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...