|
| 1 | +--- |
| 2 | +name: socialite-development |
| 3 | +description: "Manages OAuth social authentication with Laravel Socialite. Activate when adding social login providers; configuring OAuth redirect/callback flows; retrieving authenticated user details; customizing scopes or parameters; setting up community providers; testing with Socialite fakes; or when the user mentions social login, OAuth, Socialite, or third-party authentication." |
| 4 | +license: MIT |
| 5 | +metadata: |
| 6 | + author: laravel |
| 7 | +--- |
| 8 | + |
| 9 | +# Socialite Authentication |
| 10 | + |
| 11 | +## Documentation |
| 12 | + |
| 13 | +Use `search-docs` for detailed Socialite patterns and documentation (installation, configuration, routing, callbacks, testing, scopes, stateless auth). |
| 14 | + |
| 15 | +## Available Providers |
| 16 | + |
| 17 | +Built-in: `facebook`, `twitter`, `twitter-oauth-2`, `linkedin`, `linkedin-openid`, `google`, `github`, `gitlab`, `bitbucket`, `slack`, `slack-openid`, `twitch` |
| 18 | + |
| 19 | +Community: 150+ additional providers at [socialiteproviders.com](https://socialiteproviders.com). For provider-specific setup, use `WebFetch` on `https://socialiteproviders.com/{provider-name}`. |
| 20 | + |
| 21 | +Configuration key in `config/services.php` must match the driver name exactly — note the hyphenated keys: `twitter-oauth-2`, `linkedin-openid`, `slack-openid`. |
| 22 | + |
| 23 | +Twitter/X: Use `twitter-oauth-2` (OAuth 2.0) for new projects. The legacy `twitter` driver is OAuth 1.0. Driver names remain unchanged despite the platform rebrand. |
| 24 | + |
| 25 | +Community providers differ from built-in providers in the following ways: |
| 26 | +- Installed via `composer require socialiteproviders/{name}` |
| 27 | +- Must register via event listener — NOT auto-discovered like built-in providers |
| 28 | +- Use `search-docs` for the registration pattern |
| 29 | + |
| 30 | +## Adding a Provider |
| 31 | + |
| 32 | +### 1. Configure the provider |
| 33 | + |
| 34 | +Add the provider's `client_id`, `client_secret`, and `redirect` to `config/services.php`. The config key must match the driver name exactly. |
| 35 | + |
| 36 | +### 2. Create redirect and callback routes |
| 37 | + |
| 38 | +Two routes are needed: one that calls `Socialite::driver('provider')->redirect()` to send the user to the OAuth provider, and one that calls `Socialite::driver('provider')->user()` to receive the callback and retrieve user details. |
| 39 | + |
| 40 | +### 3. Authenticate and store the user |
| 41 | + |
| 42 | +In the callback, use `updateOrCreate` to find or create a user record from the provider's response (`id`, `name`, `email`, `token`, `refreshToken`), then call `Auth::login()`. |
| 43 | + |
| 44 | +### 4. Customize the redirect (optional) |
| 45 | + |
| 46 | +- `scopes()` — merge additional scopes with the provider's defaults |
| 47 | +- `setScopes()` — replace all scopes entirely |
| 48 | +- `with()` — pass optional parameters (e.g., `['hd' => 'example.com']` for Google) |
| 49 | +- `asBotUser()` — Slack only; generates a bot token (`xoxb-`) instead of a user token (`xoxp-`). Must be called before both `redirect()` and `user()`. Only the `token` property will be hydrated on the user object. |
| 50 | +- `stateless()` — for API/SPA contexts where session state is not maintained |
| 51 | + |
| 52 | +### 5. Verify |
| 53 | + |
| 54 | +1. Config key matches driver name exactly (check the list above for hyphenated names) |
| 55 | +2. `client_id`, `client_secret`, and `redirect` are all present |
| 56 | +3. Redirect URL matches what is registered in the provider's OAuth dashboard |
| 57 | +4. Callback route handles denied grants (when user declines authorization) |
| 58 | + |
| 59 | +Use `search-docs` for complete code examples of each step. |
| 60 | + |
| 61 | +## Additional Features |
| 62 | + |
| 63 | +Use `search-docs` for usage details on: `enablePKCE()`, `userFromToken($token)`, `userFromTokenAndSecret($token, $secret)` (OAuth 1.0), retrieving user details. |
| 64 | + |
| 65 | +User object: `getId()`, `getName()`, `getEmail()`, `getAvatar()`, `getNickname()`, `token`, `refreshToken`, `expiresIn`, `approvedScopes` |
| 66 | + |
| 67 | +## Testing |
| 68 | + |
| 69 | +Socialite provides `Socialite::fake()` for testing redirects and callbacks. Use `search-docs` for faking redirects, callback user data, custom token properties, and assertion methods. |
| 70 | + |
| 71 | +## Common Pitfalls |
| 72 | + |
| 73 | +- Config key must match driver name exactly — hyphenated drivers need hyphenated keys (`linkedin-openid`, `slack-openid`, `twitter-oauth-2`). Mismatch silently fails. |
| 74 | +- Every provider needs `client_id`, `client_secret`, and `redirect` in `config/services.php`. Missing any one causes cryptic errors. |
| 75 | +- `scopes()` merges with defaults; `setScopes()` replaces all scopes entirely. |
| 76 | +- Missing `stateless()` in API/SPA contexts causes `InvalidStateException`. |
| 77 | +- Redirect URL in `config/services.php` must exactly match the provider's OAuth dashboard (including trailing slashes and protocol). |
| 78 | +- Do not pass `state`, `response_type`, `client_id`, `redirect_uri`, or `scope` via `with()` — these are reserved. |
| 79 | +- Community providers require event listener registration via `SocialiteWasCalled`. |
| 80 | +- `user()` throws when the user declines authorization. Always handle denied grants. |
0 commit comments