Implement social login with OAuth 2.0 (GitHub, Google, Facebook, and more) using Laravel Socialite. This guide covers installation, configuration, the authentication flow, and database integration.
Laravel Socialite is an official package that makes it simple to implement social login via OAuth 2.0. It supports major providers such as GitHub, Google, Facebook, X (Twitter), LinkedIn, and more — turning what would otherwise be complex OAuth flows into just a few lines of code.The following providers are supported out of the box:
OAuth authentication requires two routes: one to redirect the user to the provider, and one to handle the callback.
use Laravel\Socialite\Facades\Socialite;// Redirect the user to GitHubRoute::get('/auth/github', function () { return Socialite::driver('github')->redirect();});// Handle the GitHub callbackRoute::get('/auth/github/callback', function () { $user = Socialite::driver('github')->user(); // $user->token contains the access token});
// database/migrations/xxxx_xx_xx_add_github_columns_to_users_table.phpreturn new class extends Migration{ public function up(): void { Schema::table('users', function (Blueprint $table) { $table->string('github_id')->nullable()->unique()->after('id'); $table->string('github_token')->nullable()->after('github_id'); $table->string('github_refresh_token')->nullable()->after('github_token'); }); } public function down(): void { Schema::table('users', function (Blueprint $table) { $table->dropColumn(['github_id', 'github_token', 'github_refresh_token']); }); }};
Use the with() method to include additional parameters in the redirect request:
// Restrict to a specific Google Workspace domainreturn Socialite::driver('google') ->with(['hd' => 'example.com']) ->redirect();// Always show the Google consent screenreturn Socialite::driver('google') ->with(['prompt' => 'consent']) ->redirect();
When using with(), be careful not to pass reserved keywords such as state or response_type.
To generate a Slack bot token instead of a user token, use asBotUser():
// When redirectingreturn Socialite::driver('slack') ->asBotUser() ->setScopes(['chat:write', 'chat:write.public', 'chat:write.customize']) ->redirect();// When handling the callback$user = Socialite::driver('slack')->asBotUser()->user();
use Laravel\Socialite\Facades\Socialite;test('user is redirected to GitHub', function () { Socialite::fake('github'); $response = $this->get('/auth/github'); $response->assertRedirect();});
When you need a provider that isn’t built in, registering a custom driver via Socialite::extend() is the official, recommended approach. Because SocialiteManager extends Illuminate\Support\Manager, the extension mechanism works exactly like any other Laravel driver system.
For a deeper look at the Manager pattern and how extend() works under the hood, see the Manager class guide.
Extend Laravel\Socialite\Two\AbstractProvider and implement the four abstract methods:
// app/Socialite/ExampleProvider.phpnamespace App\Socialite;use Laravel\Socialite\Two\AbstractProvider;use Laravel\Socialite\Two\User;class ExampleProvider extends AbstractProvider{ // Authorization URL — where the user is redirected to log in public function getAuthUrl($state): string { return $this->buildAuthUrlFromBase('https://example.com/oauth/authorize', $state); } // Token endpoint — exchanges the authorization code for an access token protected function getTokenUrl(): string { return 'https://example.com/oauth/token'; } // Fetch raw user data using the access token protected function getUserByToken($token): array { $response = $this->getHttpClient()->get('https://example.com/api/user', [ 'headers' => ['Authorization' => 'Bearer '.$token], ]); return json_decode($response->getBody(), true); } // Map the raw user array to a Socialite User object protected function mapUserToObject(array $user): User { return (new User)->setRaw($user)->map([ 'id' => $user['id'], 'nickname' => $user['login'] ?? null, 'name' => $user['name'], 'email' => $user['email'], 'avatar' => $user['avatar_url'] ?? null, ]); }}
Here is what each method does:
Method
Purpose
getAuthUrl($state)
Returns the OAuth authorization URL to redirect the user to
getTokenUrl()
The endpoint that exchanges the authorization code for an access token
getUserByToken($token)
Calls the provider’s user API with the token and returns a raw array
mapUserToObject(array $user)
Converts the raw array into a Socialite User instance
Once registered, the custom provider is available through the same Socialite API:
// RedirectRoute::get('/auth/example', function () { return Socialite::driver('example')->redirect();});// CallbackRoute::get('/auth/example/callback', function () { $user = Socialite::driver('example')->user();});
You can also use third-party packages that register their drivers via Socialite::extend() in a service provider. Packages that follow this pattern are easy to audit and behave predictably.
The following Socialite extension packages are maintained by the owner of this site. All of them use Socialite::extend() under the hood — adding credentials to config/services.php is all you need to get started.
LINE
LINE SDK for Laravel. Includes Socialite OAuth login and Messaging API integration.
Bluesky
AT Protocol (Bluesky) integration. Supports OAuth authentication and posting.
Discord
Discord OAuth2 login.
Threads
Meta Threads integration. Supports OAuth authentication and the Threads API.
Amazon
Login with Amazon OAuth authentication.
Mastodon
OAuth login for Mastodon instances.
WordPress
OAuth login for WordPress.com and self-hosted WordPress.