2017 © Pedro Peláez
 

library socialite

A collection of OAuth 2 packages that extracts from lichv/socialite.

image

lichv/socialite

A collection of OAuth 2 packages that extracts from lichv/socialite.

  • Thursday, March 9, 2017
  • by lichv
  • Repository
  • 1 Watchers
  • 1 Stars
  • 71 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Socialite

参考 overtrue/socialite 添加 QQ公众号登录,后续添加支付宝登录, (*1)

Requirement

PHP >= 5.4

Installation

$ composer require "lichv/socialite:~0.1"

Usage

authorize.php:, (*2)

<?php
use Lichv\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:, (*3)

<?php

// ...
$user = $socialite->driver('github')->user();

$user->getId();        // 1472352
$user->getNickname();  // "lichv"
$user->getName();      // "安正超"
$user->getEmail();     // "anzhengchao@gmail.com"
...

Configuration

Now we support the following sites:, (*4)

facebook, github, google, linkedin, weibo, qq, wechat and douban., (*5)

Each drive uses the same configuration keys: client_id, client_secret, redirect., (*6)

example:, (*7)

...
  '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:, (*8)

$response = $socialite->driver('github')
                ->scopes(['scope1', 'scope2'])->redirect();

Redirect URL

You may also want to dynamic set redirect,you can use the following methods to change the redirect URL:, (*9)

$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., (*10)

Additional parameters

To include any optional parameters in the request, call the with method with an associative array:, (*11)

$response = $socialite->driver('google')
                    ->with(['hd' => 'example.com'])->redirect();

User interface

Standard user api:


$user = $socialite->driver('weibo')->user();
{
  "id": 1472352,
  "nickname": "lichv",
  "name": "安正超",
  "email": "anzhengchao@gmail.com",
  "avatar": "https://avatars.githubusercontent.com/u/1472352?v=3",
  "original": {
    "login": "lichv",
    "id": 1472352,
    "avatar_url": "https://avatars.githubusercontent.com/u/1472352?v=3",
    "gravatar_id": "",
    "url": "https://api.github.com/users/lichv",
    "html_url": "https://github.com/lichv",
    ...
  },
  "token": {
    "access_token": "5b1dc56d64fffbd052359f032716cc4e0a1cb9a0",
    "token_type": "bearer",
    "scope": "user:email"
  }
}

You can fetch the user attribute as a array key like this:, (*12)

$user['id'];        // 1472352
$user['nickname'];  // "lichv"
$user['name'];      // "安正超"
$user['email'];     // "anzhengchao@gmail.com"
...

Or using method:, (*13)

$user->getId();
$user->getNickname();
$user->getName();
$user->getEmail();
$user->getAvatar();
$user->getOriginal();
$user->getToken();// or $user->getAccessToken()

Get original response from OAuth API

The $user->getOriginal() method will return an array of the API raw response., (*14)

Get access token Object

You can get the access token instance of current session by call $user->getToken() or $user->getAccessToken() or $user['token'] ., (*15)

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., (*16)


$request = new Request(); // or use AnotherCustomRequest. $socialite = new SocialiteManager($config, $request);

Or set request to SocialiteManager instance:, (*17)

$socialite->setRequest($request);

You can get the request from SocialiteManager instance by getRequest():, (*18)

$request = $socialite->getRequest();

Set custom session manager.

By default, the SocialiteManager use Symfony\Component\HttpFoundation\Session\Session instance as session manager, you can change it as following lines:, (*19)

$session = new YourCustomSessionManager();
$socialite->getRequest()->setSession($session);

Your custom session manager must be implement the Symfony\Component\HttpFoundation\Session\SessionInterface., (*20)

Enjoy it! :heart:, (*21)

Reference

License

MIT, (*22)

The Versions

09/03 2017

dev-master

9999999-dev

A collection of OAuth 2 packages that extracts from lichv/socialite.

  Sources   Download

MIT

The Requires

 

The Development Requires

by lichv lee

09/03 2017

0.5.7

0.5.7.0

A collection of OAuth 2 packages that extracts from lichv/socialite.

  Sources   Download

MIT

The Requires

 

The Development Requires

by lichv lee

07/03 2017

0.5.6

0.5.6.0

A collection of OAuth 2 packages that extracts from lichv/socialite.

  Sources   Download

MIT

The Requires

 

The Development Requires

by lichv lee

27/02 2017

0.5.5

0.5.5.0

A collection of OAuth 2 packages that extracts from lichv/socialite.

  Sources   Download

MIT

The Requires

 

The Development Requires

by lichv lee