Socialite social login
- Configuration
- Routing
- Optional parameters
- Access scope
- No authentication status
- Retrieve user details
- Introduction
- In addition to typical form-based authentication, Laravel also provides a simple way to authenticate to OAuth providers using Laravel Socialite Convenient method. Socialite currently supports authentication for Facebook, Twitter, LinkedIn, Google, GitHub, GitLab and Bitbucket.
Drivers for other platforms can be found on the Socialite Providers community driver website.
Upgrading Socialite Login
When upgrading to a new major version of Socialite, be sure to carefully review the Upgrade Guide.
Installation
Before starting to use the social login function, add the laravel/socialite package to your project dependencies through Composer:
composer require laravel/socialite
Configuration
Before using Socialite, you will also need to add credentials for the OAuth service used by your application. These credentials should be placed in your config/services.php
configuration file and should be used with the keys facebook
, twitter
, linkedin
, google
, github
, gitlab
or bitbucket
, depending on the provider your application requires. For example:
'github' => [ 'client_id' => env('GITHUB_CLIENT_ID'), 'client_secret' => env('GITHUB_CLIENT_SECRET'), 'redirect' => 'http://your-callback-url', ],
If the value of the
redirect
item is a relative path, it will automatically resolve to the full URL.
Routing
Next, it is time to authenticate the user! This requires two routes: one to redirect the user to the OAuth provider, and another to receive the callback request from the corresponding provider after authentication is complete. Socialite can be accessed through the facade method Socialite::
: the
<?php namespace App\Http\Controllers\Auth;use Socialite; class LoginController extends Controller{ /** * Redirect the user to the GitHub authentication page. * * @return \Illuminate\Http\Response */ public function redirectToProvider() { return Socialite::driver('github')->redirect(); } /** * Obtain the user information from GitHub. * * @return \Illuminate\Http\Response */ public function handleProviderCallback() { $user = Socialite::driver('github')->user(); // $user->token; } }
redirect
method is responsible for sending the user to the OAuth provider, and the user
method Will read the incoming request and retrieve the user's information from the provider.
Of course, you also need to define routing rules in your controller method:
Route::get('login/github', 'Auth\LoginController@redirectToProvider'); Route::get('login/github/callback', 'Auth\LoginController@handleProviderCallback');
Optional parameters
Many OAuth providers support optional parameters in redirect requests. To include any optional parameters in the request, call the with
method with an associative array:
return Socialite::driver('google') ->with(['hd' => 'example.com']) ->redirect();
When using the
with
method, be careful not to pass any retains Keywords such asstate
orresponse_type
.
Access scopes
Before redirecting the user, you can also use scopes
Method to add other "scopes" to the request. This method merges all existing scopes with the scopes you provide:
return Socialite::driver('github') ->scopes(['read:user', 'public_repo']) ->redirect();
You can overwrite all existing scopes using the setScopes
method:
return Socialite::driver('github') ->setScopes(['read:user', 'public_repo']) ->redirect();
Authentication-free state
stateless
method can be used to disable session state validation. This is useful when adding social authentication to your API:
return Socialite::driver('google')->stateless()->user();
Get user instance
Once you have the user instance, you can get more user details:
$user = Socialite::driver('github')->user(); // OAuth2 Providers $token = $user->token; $refreshToken = $user->refreshToken; // not always provided $expiresIn = $user->expiresIn; // OAuth1 Providers $token = $user->token; $tokenSecret = $user->tokenSecret; // 获取所有 Providers $user->getId(); $user->getNickname(); $user->getName(); $user->getEmail(); $user->getAvatar();
Retrieve user details from the token (OAuth2)
If you already have a valid access token for a user, you can retrieve the user's details using the userFromToken
method.
$user = Socialite::driver('github')->userFromToken($token);
Retrieve user details from token and key (OAuth1)
If you already have a valid user token/key, you can Retrieve their details using the userFromTokenAndSecret
method:
$user = Socialite::driver('twitter')->userFromTokenAndSecret($token, $secret);