maer/google-auth

此包已被放弃且不再维护。作者建议使用maer/oauth2-simple-client包。
此包的最新版本(0.5.0)没有提供许可信息。

Laravel 4版本的league/oauth2-client包包装器。简单地对特定电子邮件或整个域进行白名单/黑名单。

0.5.0 2014-06-03 08:36 UTC

This package is auto-updated.

Last update: 2022-02-01 12:33:55 UTC


README

快速轻松地在您的Laravel 4应用程序中添加Google身份验证。它基本上是thephpleague/oauth2-client包的包装器。原始包使用简单,但此包添加了简单的访问控制。

您可以选择允许或阻止哪些Google账户/域。无论如何,我已经多次使用它,并且它非常方便。就像吃华夫饼一样简单!

安装

最佳方式是使用composer

将以下内容添加到您的composer.json

{
    "require": {
        "maer/google-auth": "dev-master"
    }
}

您可以将dev-master更改为当前最新版本。

设置

app/config/app.php中注册服务提供者

'providers' => array(
    'Maer\GoogleAuth\ServiceProvider',
)

配置

发布并编辑配置文件

$ php artisan config:publish maer/google-auth

编辑app/config/packages/maer/google-auth/config.php

return array(

    'client_id'    => 'YOUR_GOOGLE_CLIENT_ID',
    'secret'       => 'YOUR_GOOGLE_CLIENT_SECRET',
    'callback_url' => 'YOUR_GOOGLE_CLIENT_CALLBACK_URL',

    /*
    * Allowed accounts
    * -------------------------------------
    * Enter full e-mail addresses or entire domains.
    * If empty, all e-mail addresses will be allowed.
    */
    'allow'     => array('your-email@a-domain.com', 'another-domain.com'),

    /*
    * Disallowed accounts
    * -------------------------------------
    * Enter full e-mail addresses or entire domains.
    * If an e-mail or domain is in the allowed and disallowed,
    * it will be blocked.
    */
    'disallow'     => array('not-allowed@another-domain.com'),
);

使用

以下是一个简单示例,说明如何使用它。您应该将其放在一个具有依赖注入的控制器中。

Route::get('/', function()
{
    echo '<a href="/google-auth">Authorize</a>';
});

Route::get('/google-auth', function(){

    // Get the instance of GoogleAuth
    $Auth = App::make('Maer\GoogleAuth\GoogleAuth');

    // This call will redirect the user to Googles Auth screen
    return $Auth->authorize();

});

Route::get('/google-auth/callback', function(){

    // Get the instance of GoogleAuth
    $Auth = App::make('Maer\GoogleAuth\GoogleAuth');
    
    // If the authorization fails, this method will return null.
    // Now it's up to you to decide what to do with the user object.
    $User = $Auth->callback();    

    // You can also ask for the access token, regardless if you use
    // the callback-method or not.
    $token = $Auth->getAccessToken();

});