GithubHelp home page GithubHelp logo

iwillhappy1314 / socialite Goto Github PK

View Code? Open in Web Editor NEW

This project forked from overtrue/socialite

0.0 1.0 0.0 221 KB

:octocat: Socialite is an OAuth2 Authentication tool. It is inspired by laravel/socialite, you can easily use it without Laravel.

License: MIT License

PHP 100.00%

socialite's Introduction

Socialite

Build Status Latest Stable Version Latest Unstable Version Build Status Scrutinizer Code Quality Code Coverage Total Downloads License

Socialite is an OAuth2 Authentication tool. It is inspired by laravel/socialite, You can easily use it in any PHP project.

Requirement

PHP >= 7.0

Installation

$ composer require "overtrue/socialite" -vvv

Usage

For Laravel 5: overtrue/laravel-socialite

authorize.php:

<?php

use Wenprise\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 Wenprise\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();  // "overtrue"
$user->getUsername();  // "overtrue"
$user->getName();      // "安正超"
$user->getEmail();     // "[email protected]"
$user->getProviderName(); // GitHub
...

Configuration

Now we support the following sites:

facebook, 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": "overtrue",
  "name": "安正超",
  "email": "[email protected]",
  "avatar": "https://avatars.githubusercontent.com/u/1472352?v=3",
  "original": {
    "login": "overtrue",
    "id": 1472352,
    "avatar_url": "https://avatars.githubusercontent.com/u/1472352?v=3",
    "gravatar_id": "",
    "url": "https://api.github.com/users/overtrue",
    "html_url": "https://github.com/overtrue",
    ...
  },
  "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'];  // "overtrue"
$user['name'];      // "安正超"
$user['email'];     // "[email protected]"
...

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

PHP 扩展包开发

想知道如何从零开始构建 PHP 扩展包?

请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— 《PHP 扩展包实战教程 - 从入门到发布》

License

MIT

socialite's People

Contributors

ac1982 avatar andares avatar aufree avatar bangbangda avatar dependabot[bot] avatar diegocardoso93 avatar eddiclin avatar flutterbest avatar hedeqiang avatar her-cat avatar hotrush avatar icebay avatar iwillhappy1314 avatar khsing avatar littlesqx avatar mingyoung avatar overtrue avatar phoenixgao avatar renweihua avatar summerblue avatar thisliu avatar xiaohome avatar xjchengo avatar xofers avatar yuqi avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.