Laravel 5 的社交 OAuth 身份验证。

1.0.3 2017-03-20 01:31 UTC

This package is not auto-updated.

Last update: 2024-09-18 20:23:08 UTC


README

安装

$ composer require "overtrue/laravel-socialite:~1.0"

如果您已安装了 overtrue/socialite 包,请在运行此命令前将其从 composer.json 中移除。

配置

  1. 安装 Socialite 库后,在您的 config/app.php 配置文件中注册 Overtrue\LaravelSocialite\ServiceProvider
'providers' => [
    // Other service providers...
    Overtrue\LaravelSocialite\ServiceProvider::class,
],
  1. 将以下行添加到 config/app.phpaliases 部分:
'Socialite' => Overtrue\LaravelSocialite\Socialite::class,
  1. 您还需要为应用程序使用的 OAuth 服务添加凭证。这些凭证应放在您的 config/socialite.phpconfig/services.php 配置文件中,并应使用 keyfacebook、twitter、linkedin、google、github 或 bitbucket,具体取决于应用程序所需的提供者。例如:
<?php

return [
    //...
    'github' => [
        'client_id'     => 'your-app-id',
        'client_secret' => 'your-app-secret',
        'redirect'      => 'https:///socialite/callback.php',
    ],
    //...
];

使用方法

<?php

namespace App\Http\Controllers;

use Socialite;
use Illuminate\Routing\Controller;

class AuthController extends Controller
{
    /**
     * Redirect the user to the GitHub authentication page.
     *
     * @return Response
     */
    public function redirectToProvider()
    {
        return Socialite::driver('github')->redirect();
    }

    /**
     * Obtain the user information from GitHub.
     *
     * @return Response
     */
    public function handleProviderCallback()
    {
        $user = Socialite::driver('github')->user();

        // $user->token;
    }
}

并注册路由

Route::get('/oauth/github', 'AuthController@redirectToProvider');
Route::get('/oauth/github/callback', 'AuthController@handleProviderCallback');

关于更多使用方法,请参阅 overtrue/socialite

许可证

MIT