Laravel login with google, facebook github and twitter using socialite?

Today we are going to learning how to authenticate application with google and facebook and other website, using socialite.

First you create laravel application using this command,

if you have installed laravel installer in your composer,

laravel new project_name

composer create-project --prefer-dist laravel/laravel blog
After the complete installation of project install  socialite package

composer require laravel/socialite
After the complete installation of socialite package add service provider in your config file config/app.php

'providers' => [
    // Other service providers...


Also, add the Socialite facade to the aliases array in your app configuration file:

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

then after you will need to add creadentials for OAuth services to authenticate 
 config/services.php Configuration ,  and should use the key facebooktwitterlinkedingooglegithub or bitbucket, depending on the providers your application requires. For example:

'facebook' => [
    'client_id' => 'your-github-app-id',
    'client_secret' => 'your-github-app-secret',
    'redirect' => 'http://your-callback-url',

Now you have configured your application for authenticate 


namespace App\Http\Controllers\Auth;

use Socialite;

class LoginController extends Controller
     * Redirect the user to the GitHub authentication page.
     * @return Response
    public function redirectToProvider()
        return Socialite::driver('facebook')->redirect();

     * Obtain the user information from GitHub.
     * @return Response
    public function handleProviderCallback()
        $user = Socialite::driver('facebook')->user();

        // $user->token;

Create route for the redirect link

Route::get('login/facebook', 'Auth\LoginController@redirectToProvider');
Route::get('login/facebook/callback', 'Auth\LoginController@handleProviderCallback');

You will retrieving data like this:

$user = Socialite::driver('facebook')->user();

// OAuth Two Providers
$token = $user->token;
$refreshToken = $user->refreshToken; // not always provided
$expiresIn = $user->expiresIn;

// OAuth One Providers
$token = $user->token;
$tokenSecret = $user->tokenSecret;

// All Providers

No comments

Powered by Blogger.