gutter/hybridauth

此包的最新版本(v2.0.5)没有可用的许可证信息。

hybridauth/hybridauth 对 Nette 框架的集成

v2.0.5 2018-04-18 14:32 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:41:54 UTC


README

这是 hybridauth/hybridauth 对 Nette 框架的集成。

安装

安装 Gutter\HybridAuth 的最佳方式是通过 Composer

$ composer require gutter/hybridauth

用法

您可以将 HybridAuth 作为扩展使用。

配置

首先您需要在 config.neon 中设置扩展。

extensions:
    hybridAuth: Gutter\HybridAuth\DI\HybridAuthExtension

hybridAuth:
    base_url: https://myapp.com/auth/process
    providers:
        Google:
            enabled: true
            keys:
                id: [your-google-key]
                secret: [your-google-secret]
        Facebook:
            enabled: true
            keys:
                id: [your-facebook-key]
                secret: [your-facebook-secret]
            scope: email

实现

然后,/auth 控制器的实现可能如下所示

class AuthPresenter extends BasePresenter
{
    /** @var \Gutter\HybridAuth\Manager @inject */
    public $hybridAuth;

    /** @var AuthModel @inject */
    public $model;

    public function actionProcess()
    {
        $this->hybridAuth->process();
    }

    public function actionGoogle()
    {
        $adapter = $this->hybridAuth->authenticate('Google');
        $userProfile = $adapter->getUserProfile();

        $user = $this->model->getUserByEmail($userProfile->email);
        if ($user) {
            $this->login($user);
        }

        $this->redirect('Login:failed');
    }

    public function actionFacebook()
    {
        $adapter = $this->hybridAuth->authenticate('Facebook');
        $userProfile = $adapter->getUserProfile();

        $user = $this->model->getUserByEmail($userProfile->email);
        if ($user) {
            $this->login($user);
        }

        $this->redirect('Login:failed');
    }
}