Laravel 5.5 Socialite for all

Create a Laravel Authentication

php artisan make:auth

Download the laravel socialite package

composer require laravel/socialite

Configuration

These credentials should be placed in your config/services.php configuration file

    'facebook' => [
        'client_id' => '337690530037',         // Your Facebook Client ID
        'client_secret' => 'f4c9f04c3344c7ef222526e2cc506', // Your Facebook Client Secret
        'redirect' => 'http://localhost:8000/login/facebook/callback',
    ], 
    'twitter' => [
        'client_id' => 'ZcY9XQU3kKJ8pktOhSgjWNJ',         // Your twitter Client ID
        'client_secret' => 'cK0m6pUZJrGjpslvgHWXVjz88q0ND7FAyEzcMi', // Your twitter Client Secret
        'redirect' => 'http://127.0.0.1:8000/login/twitter/callback',
    ],
    'google' => [
        'client_id' => '857054384681-jvq9c3bvtho429b7.apps.googleusercontent.com',   // Your google Client ID
        'client_secret' => 'MOG_QYsclYiZ11wXxqTrtn', // Your google Client Secret
        'redirect' => 'http://localhost:8000/login/google/callback',
    ],

Routing

Next, you are ready to authenticate users! You will need two routes: one for redirecting the user to the OAuth provider, and another for receiving the callback from the provider after authentication. We will access Socialite using the Socialite facade:

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Facades\Socialite;

class LoginController extends Controller
{
    
    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

   /**
     * Redirect the user to the authentication page.
     *
     * @return \Illuminate\Http\Response
     */
    public function redirectToProvider($provider)
    {
        return Socialite::driver($provider)->redirect();
    }


    /**
     * Obtain the user information from authentication.
     *
     * @return \Illuminate\Http\Response
     */
    public function handleProviderCallback($provider)
    {
        if ($provider == 'twitter') {
            $user = Socialite::driver('twitter')->user();
        } elseif($provider == 'facebook') {
            $user = Socialite::driver('facebook')->user();            
        }else{
            $user = Socialite::driver('google')->stateless()->user();
        }       
                
        $findUser = User::where('email', $user->email)->first();
        if ($findUser) {
            Auth::login($findUser);            
        } else {
            $newuser           = new User;
            $newuser->name     = $user->name;
            $newuser->email    = $user->email;
            $newuser->password = bcrypt(123456);
            $newuser->save();
            Auth::login($newuser);            
        }
        return redirect('home');
    }

}

Of course, you will need to define routes to your routing file routes > web.php

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

Edit your existing login view resources/views/auth/login.blade.php and add following HTML code.

<div class="panel-body">              
    <div class="panel-body">
        <a class="btn btn-primary" href="{{'/login/facebook'}}">
           {{'Facebook Login'}}
        </a>
        <a class="btn btn-primary" href="{{'/login/twitter'}}">
            {{'Twitter Login'}}
        </a>
        <a class="btn btn-primary" href="{{'/login/google'}}">
            {{'Google Plus'}}
        </a>
    </div>
</div>   
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s