ezreal / socialite
A collection of OAuth 2 packages that extracts from laravel/socialite.
Requires
- php: >=7.0
- guzzlehttp/guzzle: ~5.0|~6.0
- symfony/http-foundation: ^2.7|^3.0|^4.0
Requires (Dev)
- mockery/mockery: ~1.2
- phpunit/phpunit: ~6
This package is auto-updated.
Last update: 2024-11-06 14:13:25 UTC
README
该包在overtrue/socialite
基础上修改 新增line
登录
Requirement
PHP >= 7.0
Installation
composer install ezreal/socialite
Usage
authorize.php
:
<?php use Ezreal\Socialite\SocialiteManager; $config = [ 'github' => [ 'client_id' => 'your-app-id', 'client_secret' => 'your-app-secret', 'redirect' => 'http://localhost/socialite/callback.php', ], ]; $socialite = new SocialiteManager($config); $response = $socialite->driver('github')->redirect(); echo $response;// or $response->send();
callback.php
:
<?php use Ezreal\Socialite\SocialiteManager; $config = [ 'github' => [ 'client_id' => 'your-app-id', 'client_secret' => 'your-app-secret', 'redirect' => 'http://localhost/socialite/callback.php', ], ]; $socialite = new SocialiteManager($config); $user = $socialite->driver('github')->user(); $user->getId(); // 1472352 $user->getNickname(); // "Ezreal" $user->getUsername(); // "Ezreal" $user->getName(); // "安正超" $user->getEmail(); // "anzhengchao@gmail.com" $user->getProviderName(); // GitHub ...
Configuration
下方这些东西暂时证明没有什么卵用 请勿用
Now we support the following sites:
facebook
, line
, github
, google
, linkedin
, outlook
, weibo
, taobao
, qq
, wechat
, douyin
, and douban
.
Each driver uses the same configuration keys: client_id
, client_secret
, redirect
.
Example:
...
'weibo' => [
'client_id' => 'your-app-id',
'client_secret' => 'your-app-secret',
'redirect' => 'http://localhost/socialite/callback.php',
],
...
Scope
Before redirecting the user, you may also set "scopes" on the request using the scope method. This method will overwrite all existing scopes:
$response = $socialite->driver('github') ->scopes(['scope1', 'scope2'])->redirect();
Redirect URL
You may also want to dynamicly set redirect
,you can use the following methods to change the redirect
URL:
$socialite->redirect($url); // or $socialite->withRedirectUrl($url)->redirect(); // or $socialite->setRedirectUrl($url)->redirect();
WeChat scopes:
snsapi_base
,snsapi_userinfo
- Used to Media Platform Authentication.snsapi_login
- Used to web Authentication.
Additional parameters
To include any optional parameters in the request, call the with method with an associative array:
$response = $socialite->driver('google') ->with(['hd' => 'example.com'])->redirect();
User interface
Standard user api:
$user = $socialite->driver('weibo')->user();
{ "id": 1472352, "nickname": "Ezreal", "name": "安正超", "email": "anzhengchao@gmail.com", "avatar": "https://avatars.githubusercontent.com/u/1472352?v=3", "original": { "login": "Ezreal", "id": 1472352, "avatar_url": "https://avatars.githubusercontent.com/u/1472352?v=3", "gravatar_id": "", "url": "https://api.github.com/users/Ezreal", "html_url": "https://github.com/Ezreal", ... }, "token": { "access_token": "5b1dc56d64fffbd052359f032716cc4e0a1cb9a0", "token_type": "bearer", "scope": "user:email" } }
You can fetch the user attribute as a array keys like these:
$user['id']; // 1472352 $user['nickname']; // "Ezreal" $user['name']; // "安正超" $user['email']; // "anzhengchao@gmail.com" ...
Or using the method:
$user->getId(); $user->getNickname(); $user->getName(); $user->getEmail(); $user->getAvatar(); $user->getOriginal(); $user->getToken();// or $user->getAccessToken() $user->getProviderName(); // GitHub/Google/Facebook...
Get original response from OAuth API
The $user->getOriginal()
method will return an array of the API raw response.
Get access token Object
You can get the access token instance of current session by call $user->getToken()
or $user->getAccessToken()
or $user['token']
.
Get user with access token
$accessToken = new AccessToken(['access_token' => $accessToken]); $user = $socialite->user($accessToken);
Custom Session or Request instance.
You can set the request with your custom Request
instance which instanceof Symfony\Component\HttpFoundation\Request
before you call driver
method.
$request = new Request(); // or use AnotherCustomRequest. $socialite = new SocialiteManager($config, $request);
Or set request to SocialiteManager
instance:
$socialite->setRequest($request);
You can get the request from the SocialiteManager
instance by getRequest()
:
$request = $socialite->getRequest();
Set custom session manager.
By default, the SocialiteManager
uses the Symfony\Component\HttpFoundation\Session\Session
instance as session manager, you can change it as follows:
$session = new YourCustomSessionManager(); $socialite->getRequest()->setSession($session);
Your custom session manager must be implement the
Symfony\Component\HttpFoundation\Session\SessionInterface
.
Enjoy it! ❤️
Reference
- Google - OpenID Connect
- Facebook - Graph API
- Linkedin - Authenticating with OAuth 2.0
- 微博 - OAuth 2.0 授权机制说明
- QQ - OAuth 2.0 登录QQ
- 微信公众平台 - OAuth文档
- 微信开放平台 - 网站应用微信登录开发指南
- 微信开放平台 - 代公众号发起网页授权
- 豆瓣 - OAuth 2.0 授权机制说明
- 抖音 - 网站应用开发指南
PHP 扩展包开发
想知道如何从零开始构建 PHP 扩展包?
请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— 《PHP 扩展包实战教程 - 从入门到发布》
License
MIT