her-cat/colourlife-oauth2

基于 overtrue/socialite 的彩之云 OAuth 2.0 授权 SDK

1.0 2019-10-11 09:07 UTC

This package is auto-updated.

Last update: 2025-03-11 21:29:23 UTC


README

基于 overtrue/socialite 的彩之云 OAuth 2.0 SDK

Build Status StyleCI build status

环境要求

PHP >= 7.0

安装

$ composer require her-cat/colourlife-oauth2 -vvv

单元测试

$ composer test

使用

基本使用

authorize.php:

<?php

require_once './vendor/autoload.php';

use HerCat\ColourlifeOauth2\ColourlifeOAuth2Provider;
use Overtrue\Socialite\SocialiteManager;

$config = [
    'colourlife' => [
        'client_id' => 'your-ice-application-id',
        'client_secret' => 'your-ice-secret',
        'redirect' => 'http://localhost/callback.php',
        'environment' => 'prod',
    ],
];

$socialite = new SocialiteManager($config);

$socialite->extend('colourlife', function ($config) use ($socialite) {
    $config = $config['colourlife'] ?? [];

    /** @var ColourlifeOAuth2Provider $provider */
    $provider = $socialite->buildProvider(ColourlifeOAuth2Provider::class, $config);

    return $provider->environment($config['environment'] ?? 'prod');
});

$response = $socialite->driver('colourlife')->redirect();

$response->send();

callback.php:

<?php

require_once './vendor/autoload.php';

use HerCat\ColourlifeOauth2\ColourlifeOAuth2Provider;
use Overtrue\Socialite\SocialiteManager;

$config = [
    'colourlife' => [
        'client_id' => 'your-ice-application-id',
        'client_secret' => 'your-ice-secret',
        'redirect' => 'http://localhost/callback.php',
        'environment' => 'prod',
    ],
];

$socialite = new SocialiteManager($config);

$socialite->extend('colourlife', function ($config) use ($socialite) {
    $config = $config['colourlife'] ?? [];

    /** @var ColourlifeOAuth2Provider $provider */
    $provider = $socialite->buildProvider(ColourlifeOAuth2Provider::class, $config);

    return $provider->environment($config['environment'] ?? 'prod');
});

$user = $socialite->driver('colourlife')->user();

$user->getId();                 // xxxxxxxxxx
$user->getName();               // her-cat
$user->getProviderName();       // colourlife
$user->getAttribute('mobile');  // 18500000001

在 Laravel 中使用

配置

  1. config/app.php 中注册 HerCat\ColourlifeOAuth2\ServiceProvider:
'providers' => [
    // Other service providers...
    HerCat\ColourlifeOAuth2\ServiceProvider::class,
],
  1. 将下面这一行添加到 config/app.phpaliases 部分:
'aliases' => [
    // Other aliases...
    'ColourlifeOAuth2' => HerCat\ColourlifeOAuth2\Facades\ColourlifeOAuth2::class,
],
  1. config/socialite.phpconfig/services.php 文件中配置 OAuth 服务凭证:
<?php

return [
    
    //...
    
    'colourlife' => [
        'client_id' => 'your-ice-application-id',
        'client_secret' => 'your-ice-secret',
        'redirect' => config('app.url').'/oauth/colourlife/callback',
        'environment' => 'prod', // OAuth 服务环境,dev: 测试,prod:正式
    ],
    
    //...
    
];

使用

<?php

namespace App\Http\Controllers;

use HerCat\ColourlifeOAuth2\Facades\ColourlifeOAuth2;

class AuthController extends Controller
{
    public function redirect()
    {
        return ColourlifeOAuth2::redirect();
    }

    public function handleCallback()
    {
        $user = ColourlifeOAuth2::user();

        print_r($user->getName());
    }
}

添加路由:

Route::get('/oauth/colourlife', 'AuthController@redirect');
Route::get('/oauth/colourlife/callback', 'AuthController@handleCallback');

有关更多用法,请参阅 overtrue/socialite

参考

License

MIT