Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

php - Using Laravel Socialite to login to facebook

I am new to Laravel however and I am following the tutorial on http://www.codeanchor.net/blog/complete-laravel-socialite-tutorial/, to login a user through Facebook into my application. However, almost everywhere I find a tutorial using either Github or Twitter for the Socialite Plugin provided in Laravel.

My problem is that on following everything in the tutorial, as I click on the "Login to Facebook" button, it throws an "Invalid Argument Exception" with No Socialite driver was specified.".

Another stack overflow question seemed to narrow things down: https://stackoverflow.com/questions/29673898/laravel-socialite-invalidargumentexception-in-socialitemanager-php-line-138-n

Stating that the problem is in the config/services.php

Now, i have the app_id and app_secret. However, the redirect link seems to be confusing as I can't find it on Facebook either. I am aware that this is where my app should go to Facebook for login, however, unsure of what it should be.

Does anyone have any idea on this.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

In your composer.json add- "laravel/socialite": "~2.0",

"require": {
        "laravel/framework": "5.0.*",
        "laravel/socialite": "~2.0",

the run composer update

In config/services.php add:

//Socialite
    'facebook' => [
        'client_id'     => '1234567890444',
        'client_secret' => '1aa2af333336fffvvvffffvff',
        'redirect'      => 'http://laravel.dev/login/callback/facebook',
    ],

You need to create two routes, mine are like these:

//Social Login
Route::get('/login/{provider?}',[
    'uses' => 'AuthController@getSocialAuth',
    'as'   => 'auth.getSocialAuth'
]);


Route::get('/login/callback/{provider?}',[
    'uses' => 'AuthController@getSocialAuthCallback',
    'as'   => 'auth.getSocialAuthCallback'
]);

You also need to create controller for the routes above like so:

<?php namespace AppHttpControllers;

 use LaravelSocialiteContractsFactory as Socialite;

 class AuthController extends Controller
 {

       public function __construct(Socialite $socialite){
           $this->socialite = $socialite;
       }


       public function getSocialAuth($provider=null)
       {
           if(!config("services.$provider")) abort('404'); //just to handle providers that doesn't exist

           return $this->socialite->with($provider)->redirect();
       }


       public function getSocialAuthCallback($provider=null)
       {
          if($user = $this->socialite->with($provider)->user()){
             dd($user);
          }else{
             return 'something went wrong';
          }
       }

 }

and finally add Site URL to your Facebook App like so:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...