Why Use Social Login with Twitter (X) in Laravel Applications?
Twitter (X) has millions of active users worldwide. Adding X login (formerly Twitter login) to your Laravel app offers:
- Faster registration and login
- No need for users to create new credentials
- Higher conversion rates
- Access to basic profile data (name, username, email*)
*Note: Email access requires special approval in X Developer Portal since 2023 changes.
Also check our guides for: Google Sign-In | Facebook Login

Table Of Content
1 Prerequisites
- PHP 8.2+
- Composer
- MySQL or preferred database
- A Twitter/X Developer Account (free tier is usually enough for auth)
2 Introduction
In today's web applications, offering seamless social login options significantly improves user experience and boosts registration rates. Instead of forcing users to create yet another account and password, you can let them sign in quickly using their existing social profiles.
In this comprehensive guide, we'll walk you through implementing Twitter (now X) authentication in a Laravel 11 application using the official Laravel Socialite package combined with the Breeze starter kit for authentication scaffolding.
Laravel Socialite is a powerful, developer-friendly package that simplifies OAuth-based authentication with various providers. It is not included by default in fresh Laravel installations, but adding it takes just a single Composer command.
As of 2026, Laravel Socialite officially supports the following providers out of the box:
- X (formerly Twitter)
- GitHub
- GitLab
- Bitbucket
- Slack
- Twitch
Additional platforms are available through the community-driven Socialite Providers ecosystem.
In this tutorial, we'll focus specifically on X (Twitter) login — including setting up the developer app in 2026, handling OAuth configuration (noting OAuth 1.0a is default, with OAuth 2.0 available), gracefully managing cases where email may not be returned due to X's privacy restrictions, and integrating everything securely with Laravel Breeze.
By the end, you'll have a fully functional "Sign in with X" button that authenticates users and creates or logs them into your application — perfect for modern Laravel projects.
Let's get started!
3 Create a New Laravel 11 Project
3.1 Install Laravel Project
Use the following command to install new Laravel Project.
composer create-project laravel/laravel twitter-auth-app
Then, navigate to your project directory:
cd twitter-auth-app
3.2 Configure MySql Database
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_x_auth
DB_USERNAME=root
DB_PASSWORD=
4 Install Required Packages
4.1 Install Breeze Package
composer require laravel/breeze --dev
After Composer has installed the Laravel Breeze package, you should run the breeze:install Artisan command. This command publishes the authentication views, routes, controllers, and other resources to your application.
php artisan breeze:install
php artisan migrate
npm install
npm run dev
4.2 Install Socialite Package
composer require laravel/socialite
5 Create Twitter (X) Developer Application
5.1 Login into Twitter Developer Account
5.2 Create an Application



5.3 Setup User Authentication Settings




6 Configure Credentials in Laravel
6.1 Add the Twitter API Credentials in .env
TWITTER_CLIENT_ID=Your Client ID
TWITTER_CLIENT_SECRET=Your Client Secret
TWITTER_REDIRECT_URI=http://127.0.0.1:8000/callback/twitter
6.2 Add the Twitter API .env key in config/services.php
<?php
return [
// Other services ..
'twitter' => [
'client_id' => env('TWITTER_CLIENT_ID'),
'client_secret' => env('TWITTER_CLIENT_SECRET'),
'redirect' => env('TWITTER_REDIRECT_URI'),
],
];
?>
6.3 Add New Column within the Users Table to Store twitter_id
php artisan make:migration add_twitter_id_to_users
In the generated new migration file, update the up and down methods as described below:
database/migrations/2024_05_24_120820_add_twitter_id_to_users.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('twitter_id')->after('password')->nullable()->unique();
});
}
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('twitter_id');
});
}
};
Use the following command to run the migration to update your database.
php artisan migrate
6.4 Update the User Model
app/Models/User.php
<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'email',
'password',
'twitter_id'
];
/**
* The attributes that should be hidden for serialization.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* Get the attributes that should be cast.
*
* @return array
*/
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
}
}
?>
7 Create New Controller - TwitterLoginController
Use the following artisan command to Create Controller.
php artisan make:controller TwitterSocialiteController
app/Http/Controllers/TwitterLoginController.php
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Models\User;
use Auth;
use Exception;
use Socialite;
class TwitterSocialiteController extends Controller
{
public function redirectToTwitter()
{
// redirect user to "login with Twitter account" page
return Socialite::driver('twitter')----->redirect();
}
public function handleCallback()
{
try {
// get user data from Twitter
$user = Socialite::driver('twitter')->user();
// find user in the database where the social id is the same with the id provided by Twitter
$finduser = User::where('twitter_id', $user->id)->first();
if ($finduser) // if user found then do this
{
// Log the user in
Auth::login($finduser);
// redirect user to dashboard page
return redirect('/dashboard');
}
else
{
// if user not found then this is the first time he/she try to login with Twitter account
// create user data with their Twitter account data
$newUser = User::create([
'name' => $user->name,
'email' => $user->email,
'twitter_id' => $user->id,
'password' => bcrypt('my-twitter'), // fill password by whatever pattern you choose
]);
Auth::login($newUser);
return redirect('/dashboard');
}
}
catch (Exception $e)
{
dd($e->getMessage());
}
}
}
?>
8 Define a Route for Socialite Login with Twitter
routes/web.php
Route::get('auth/twitter', [TwitterSocialiteController::class, 'redirectToTwitter']); // redirect to Twitter login
Route::get('callback/twitter', [TwitterSocialiteController::class, 'handleCallback']); // callback route after Twitter account chosen
9 Add Twitter Login Button to View
resources/views/auth/login.blade.php
<a href="{{ url('auth/twitter') }}">
<img src="images/twitter_button.png">
</a>
10 Folder Structure
11 Test the Twitter (X) Authentication
php artisan serve
Visit: http://127.0.0.1:8000/login → Click Twitter button → Authorize → Redirect to dashboard.
Common Issues & Troubleshooting
- Redirect URI Mismatch → Double-check exact URL in .env and X Developer Portal
- 401/403 Errors → Wrong keys, permissions not set, or app not approved for email
- Email is null → X restricts email; handle gracefully
- OAuth changes → For OAuth 2.0 use 'twitter-oauth-2' in config/services.php
12 Conclusion
Written by Revathi M
PHP Developer & Technical Writer · 10+ years building web applications with CodeIgniter and Laravel
Revathi specializes in PHP backend development, authentication systems, and REST API design. She writes practical, production-tested tutorials at Get Sample Code to help developers build secure applications faster.
Frequently Asked Questions
Add `TWITTER_CLIENT_ID`, `TWITTER_CLIENT_SECRET`, and `TWITTER_REDIRECT_URI=http://127.0.0.1:8000/callback/twitter` to the `.env` file. Then add the 'twitter' array to `config/services.php`.
Run `php artisan make:migration add_twitter_id_to_users`, add `$table->string('twitter_id')->nullable()->unique();` in the migration, then run `php artisan migrate`. Update the User model's `$fillable` array to include 'twitter_id'.
Run `php artisan make:controller TwitterSocialiteController`, then implement `redirectToTwitter()` to redirect using Socialite and `handleCallback()` to handle the user response, find or create the user, and log them in.
In `routes/web.php`, add `Route::get('auth/twitter', [TwitterSocialiteController::class, 'redirectToTwitter']);` and `Route::get('callback/twitter', [TwitterSocialiteController::class, 'handleCallback']);`.
In the login view (e.g., from Breeze), add <a href=`{{ url('auth/twitter') }}`><img src='images/twitter_button.png'> </a> or a similar button linking to `/auth/twitter`.
The callback URL in the Twitter app settings must exactly match the `TWITTER_REDIRECT_URI` in `.env` (e.g., `http://127.0.0.1:8000/callback/twitter`).
Twitter's OAuth 2.0 does not always return the email address. You must enable 'Request email from users' in the app settings and may need elevated access approval from Twitter.
Verify API Key/Secret, ensure the callback URL is approved in the Twitter app, and check if you're using the correct driver ('twitter' for OAuth 1.0a or consider compatibility issues with OAuth 2.0).
The tutorial uses `twitter_id` for lookup. If email is available, you can modify the query to match on email or combine both for better merging.
Run `php artisan serve`, visit `http://127.0.0.1:8000`, go to login, click the Twitter button, and complete the OAuth flow.
You need PHP 8.2 or higher, Composer, a MySQL database, and a Twitter (X) account to create a developer app.
Run `composer create-project laravel/laravel twitter-auth-app`, then `cd twitter-auth-app`.
Install Breeze with `composer require laravel/breeze --dev`, then `php artisan breeze:install`, `php artisan migrate`, `npm install`, and `npm run dev`. Install Socialite with `composer require laravel/socialite`.
Go to the Twitter Developer Portal[](https://developer.twitter.com/), create an app, select 'Web App', enable 'Request email from users', and set the callback URL to `http://127.0.0.1:8000/callback/twitter`.
