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.
Laravel Socialite is a package developed to abstract away any social authentication complexities and boilerplate code into a fluent and expressive interface. Socialite only supports Google, Facebook, Twitter, Linked In, Github, and Bitbucket as OAuth providers.
#3: Set Up Facebook Login for Your Website At this point, you'll see Facebook Login among your website app options. Click the Set Up button to get started. Next, you'll fill in the information about how and where you'll use the app. You can add the Facebook Login feature on any app across multiple devices.
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 App\Http\Controllers;
use Laravel\Socialite\Contracts\Factory 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:
It's little bit tricky for something that looks/should be easy, but anyway this is how i make things works for me.
you can find those instructions and more details in socialite docs
composer require laravel/socialite
in config/services.php add
'facebook' => [
'client_id' => env('FACEBOOK_CLIENT_ID'),
'client_secret' => env('FACEBOOK_CLIENT_SECRET'),
'redirect' => env('FACEBOOK_CALLBACK_URL'),
],
in .env file add
FACEBOOK_CLIENT_ID=paste_client_id_here
FACEBOOK_CLIENT_SECRET=paste_client_secret_here
FACEBOOK_CALLBACK_URL=https://www.example.com/auth/facebook/callback
in routes/web.php add
Route::get('auth/facebook/', 'Auth\FacebookController@redirect')->name('auth.facebook');
Route::get('auth/facebook/callback', 'Auth\FacebookController@callback')->name('auth.facebook.callback');
in App\Http\Controllers\Auth add new controller FacebookController.php
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\User;
use Socialite;
class FacebookController extends Controller {
/**
* Create a new controller instance.
*
* @return void
*/
public function redirect()
{
return Socialite::driver('facebook')->redirect();
}
/**
* Create a new controller instance.
*
* @return void
*/
public function callback(Request $request)
{
try {
$facebookAccount = Socialite::driver('facebook')->user();
// your logic here...
return redirect()->route('your.route.name');
} catch (Exception $e) {
return redirect()->route('auth.facebook');
}
}
}
go to https://developers.facebook.com/apps and create new app (if you don't have one already)
and make sur your app settings are like below in those screen shots:
If you are developing in your local machine, you have to use tools like ngrok that provide a secure link to your localhost.
In the facebook login settings change https://www.example.com with the url provided by ngrok something like https://8b0215bc.ngrok.io.
It is the only way that worked for me while developing in my local machine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With