In this tutorial, you will learn laravel 9 login with twitter. This post will give you simple example of laravel 9 login with twitter account. I’m going to show you about the laravel 9 jetstream login with Twitter. In this article, we will implement login with Twitter in laravel 9. You just need to some steps to done laravel 9 socialite login with Twitter.
As we know social media becomes more and more popular in the world. Everyone has social accounts like Gmail, Facebook, Twitter, etc. I think also most of have a Twitter account. So if your application has a login with social then it becomes awesome. you got more people to connect with your website because most of the people do not want to fill out the signup or sign-in form. If there login with social then it becomes awesome.
So if you want to also implement login with Twitter account then i will help you step by step instructions. let's follow tutorial and implement it.
Preview:
Step 1: Install Laravel 9
This is optional; however, if you have not created the laravel app, then you may go ahead and execute the below command:
Now, in this step, we need to use composer command to install jetstream, so let's run bellow command and install bellow library.
composer require laravel/jetstream
now, we need to create authentication using bellow command. you can create basic login, register and email verification. if you want to create team management then you have to pass addition parameter. you can see bellow commands:
php artisan jetstream:install livewire
Now, let's node js package:
npm install
let's run package:
npm run dev
now, we need to run migration command to create database table:
Then you need to add google client id and client secret in .env file:
.env
TWITTER_CLIENT_ID=xyzTWITTER_CLIENT_SECRET=123
Step 5: Add Database Column
In this step first we have to create migration for add twitter_id in your user table. So let's run bellow command:
php artisan make:migration add_twitter_id_column
Migration
<?php use Illuminate\Database\Migrations\Migration;use Illuminate\Database\Schema\Blueprint;use Illuminate\Support\Facades\Schema; return new class extends Migration{ /** * Run the migrations. * * @return void */ public function up() { Schema::table('users', function ($table) { $table->string('twitter_id')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { }};
Update mode like this way:
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;use Laravel\Fortify\TwoFactorAuthenticatable;use Laravel\Jetstream\HasProfilePhoto;use Laravel\Sanctum\HasApiTokens; class User extends Authenticatable{ use HasApiTokens; use HasFactory; use HasProfilePhoto; use Notifiable; use TwoFactorAuthenticatable; /** * The attributes that are mass assignable. * * @var string[] */ protected $fillable = [ 'name', 'email', 'password', 'twitter_id' ]; /** * The attributes that should be hidden for serialization. * * @var array */ protected $hidden = [ 'password', 'remember_token', 'two_factor_recovery_codes', 'two_factor_secret', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'email_verified_at' => 'datetime', ]; /** * The accessors to append to the model's array form. * * @var array */ protected $appends = [ 'profile_photo_url', ];}
Step 6: Create Routes
After adding twitter_id column first we have to add new route for twitter login. so let's add bellow route in routes.php file.
routes/web.php
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\TwitterController; /*|--------------------------------------------------------------------------| Web Routes|--------------------------------------------------------------------------|| Here is where you can register web routes for your application. These| routes are loaded by the RouteServiceProvider within a group which| contains the "web" middleware group. Now create something great!|*/ Route::get('/', function () { return view('welcome');}); Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () { return view('dashboard');})->name('dashboard'); Route::controller(TwitterController::class)->group(function(){ Route::get('auth/twitter', 'redirectToTwitter')->name('auth.twitter'); Route::get('auth/twitter/callback', 'handleTwitterCallback');});
Step 7: Create Controller
After add route, we need to add method of twitter auth that method will handle twitter callback url and etc, first put bellow code on your TwitterController.php file.
app/Http/Controllers/TwitterController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request;use Laravel\Socialite\Facades\Socialite;use Exception;use App\Models\User;use Illuminate\Support\Facades\Auth; class TwitterController extends Controller{ /** * Create a new controller instance. * * @return void */ public function redirectToTwitter() { return Socialite::driver('twitter')->redirect(); } /** * Create a new controller instance. * * @return void */ public function handleTwitterCallback() { try { $user = Socialite::driver('twitter')->user(); $finduser = User::where('twitter_id', $user->id)->first(); if($finduser){ Auth::login($finduser); return redirect()->intended('dashboard'); }else{ $newUser = User::updateOrCreate(['email' => $user->email],[ 'name' => $user->name, 'twitter_id'=> $user->id, 'password' => encrypt('123456dummy') ]); Auth::login($newUser); return redirect()->intended('dashboard'); } } catch (Exception $e) { dd($e->getMessage()); } }}
Step 8: Update Blade File
Ok, now at last we need to add blade view so first create new file login.blade.php file and put bellow code: