Posted by Arjun on Tuesday 5th June 2018

Laravel 5.6 Socialite Twitter Login

Social Logins Laravel 5.6 Socialite Twitter Login

In this post, I would like to show you, how we can use Laravel's Socialite plugin to use Twitter authentication in your Laravel based application.

To use twitter authentication APIs we need client ID and client secret and we can get these details from twitter app center. So lets login to and create a new app.

Create an application Twitter

After creating the application you can get client ID and client secret from Keys and Access Tokens tab.

Laravel application setup

Let's create a brand new Laravel project with the following command:

$ composer create-project --prefer-dist laravel/laravel laravel5.6-twitter-login

Database connection settings

After creating project open .env file and update your database credentials:


Next step, would be, go to the project root and open the terminal and type the following command. It will create the two tables, which is by default ships by Laravel 5.6

$ php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table

You might get following error :

 PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes")

to fix this, all you have to do is to edit your AppServiceProvider.php file and add to the boot method a default string length:

use Illuminate\Support\Facades\Schema;

function boot()

Lets add two new columns called provider_id, provder to the users table, create a migration file with below commnad

$php artisan make:migration add_socail_provider_columns_to_users_table
Created Migration: 2018_06_05_064007_add_socail_provider_columns_to_users_table

Open create migration file and update it as shown below


use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddSocailProviderColumnsToUsersTable extends Migration
     * Run the migrations.
     * @return void
    public function up()
        Schema::table('users', function($table) {

     * Reverse the migrations.
     * @return void
    public function down()
        Schema::table('users', function($table) {

NOTE: Changing columns for table "users" requires Doctrine DBAL; so install "doctrine/dbal" with composer

$ composer require doctrine/dbal

Again run the php artisan migrate command from the project root. It will add new columns to the user table and it will modify email and password default values to null.

Now update your user model fillable array as shown below, with new columns

     * The attributes that are mass assignable.
     * @var array
    protected $fillable = [
        'name', 'email', 'password', 'provider','provider_id'

Installation of socialite

To get started with Socialite, use Composer to add the package to your project's dependencies:

composer require laravel/socialite

Now your twitter application credentials should be placed in your config/services.php configuration file. (In real app place config values in .env file, its a good practice).

'twitter' => [
    'client_id' => 'jvVgZoMJFDGF0nuSRFzrE4jO3',
    'client_secret' => 'onPJd84dU1ZrHgsGmWCFBI4iv5octmWDLQooJ1TZix1T34ttjf',
    'redirect' => 'http://localhost:8000/auth/callback/twitter',

so let's define callback and redirect routes in routes/web.php file:

Route::get('auth/callback/{provider}', '[email protected]');
Route::get('auth/redirect/{provider}', '[email protected]');

Create a controller called SocialAuthController with following artisan command

$php artisan make:controller SocialAuthController
Controller created successfully.

Now update your controller with below code:


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Socialite;
use Laravel\Socialite\Contracts\Provider;
use App\User;
use Auth;

class SocialAuthController extends Controller
    public function callback($provider)
        $user = $this->createOrGetUser(Socialite::driver($provider));
        return redirect()->to('/');

    public function redirect($provider)
        return Socialite::driver($provider)->redirect();

    private function createOrGetUser(Provider $provider)
        $providerUser = $provider->user();

        $providerName = class_basename($provider);
        $user = User::whereProvider($providerName)
        if (!$user) {
            $user = User::create([
                'email' => $providerUser->getEmail(),
                'name' => $providerUser->getName(),
                'provider_id' => $providerUser->getId(),
                'provider' => $providerName
        return $user;

    public function logout(Request $request) {
        return redirect('/');

Now update view file welcome.blade with below code:

    <div class="flex-center position-ref full-height">
             <p>welcome {{ Auth::user()->name }}</br>
             <a href="{{ url('/auth/logout') }}">Logout</a></p>
             <a href="{{ url('auth/redirect/twitter') }}">Twitter Login</a>

That's it, now start the application with php artisan serve, and access your application http://localhost:8000.