Laravel Socialite Documentation

repository·5.x·Indexed 26 days ago

https://github.com/laravel/socialite

A package providing a fluent interface for OAuth authentication with multiple social media and service providers. It includes built-in drivers for Bitbucket, Facebook, GitHub, GitLab, Google, LinkedIn, Slack, Twitch, and X, and supports over 150 community-driven adapters. The documentation covers provider configuration in services.php, implementing redirect and callback routes, handling stateless authentication for APIs/SPAs, customizing scopes, and testing flows using Socialite::fake().

Tokens
1.9K
Snippets
0
Records
23
Agent score
81%

What's inside Laravel Socialite

  1. Introduction to Laravel Socialite

    5.x

    Laravel Socialite provides an expressive, fluent interface for OAuth authentication. It simplifies the process of implementing social authentication by handling the boilerplate code required for various providers.

    Supported official providers include:

    • Bitbucket
    • Facebook
    • GitHub
    • GitLab
    • Google
    • LinkedIn
    • Slack
    • Twitch
    • X
  2. Implement OAuth redirect and callback routes

    5.x

    A standard Socialite flow requires two routes:

    1. Redirect Route: Calls Socialite::driver('provider')->redirect() to send the user to the OAuth provider.
    2. Callback Route: Calls Socialite::driver('provider')->user() to receive the callback and retrieve user details.

    Important: In API or SPA contexts where session state is not maintained, you must call stateless() on the driver to avoid InvalidStateException.

  3. Upgrade to Socialite 4.0 from 3.x

    5.x

    When upgrading to Socialite 4.0 from version 3.x, ensure your environment meets the following minimum requirements:

    • PHP Version: 7.1.3 or higher.
    • Laravel Version: Compatible with the latest Laravel framework versions (ensure your Laravel version is compatible with PHP 7.1.3+).
  4. Troubleshoot common Socialite issues

    5.x

    Common Pitfalls

    • Config Key Mismatch: The key in config/services.php must match the driver name exactly. Hyphenated drivers like linkedin-openid or slack-openid require hyphenated keys. Mismatches fail silently.
    • Missing Credentials: Every provider requires client_id, client_secret, and redirect. Missing any of these causes cryptic errors.
    • InvalidStateException: If working in an API/SPA context, you must call stateless() on the driver.
    • Redirect URL Mismatch: The redirect value in config/services.php must be an exact match to the URL registered in the provider's dashboard.
    • Denied Grants: The user() method throws an exception if the user declines authorization. Always wrap your callback logic in a try/catch or handle denied grants appropriately.
    • Community Provider Registration: Community providers are not auto-discovered; they require registration via the SocialiteWasCalled event listener.
  5. Customize OAuth redirects with scopes and parameters

    5.x

    You can modify the redirect request using the following methods:

    • scopes(): Merges additional scopes with the provider's default scopes.
    • setScopes(): Replaces all default scopes with the provided list.
    • with(array $parameters): Passes optional parameters to the provider (e.g., ['hd' => 'example.com'] for Google). Do not pass reserved parameters like state, response_type, client_id, redirect_uri, or scope via this method.
    • asBotUser(): Slack only. Generates a bot token (xoxb-) instead of a user token (xoxp-). This must be called before both redirect() and user(). Only the token property will be hydrated.