【问题标题】:Misunderstanding of using Yii eauth extension使用 Yii eauth 扩展的误区
【发布时间】:2014-12-27 08:43:59
【问题描述】:

我在我的 yii 项目中使用yii eauth extension 通过社交帐户登录。我还阅读了来自 github 和 russian version 的文档,但我不明白如何使用该扩展。也许那个代码:

$serviceName = Yii::app()->request->getQuery('service');
if (isset($serviceName)) {
    /** @var $eauth EAuthServiceBase */
    $eauth = Yii::app()->eauth->getIdentity($serviceName);
    $eauth->redirectUrl = Yii::app()->user->returnUrl;
    $eauth->cancelUrl = $this->createAbsoluteUrl('site/sociallogin');

    try {
        if ($eauth->authenticate()) {
            //var_dump($eauth->getIsAuthenticated(), $eauth->getAttributes());
            $identity = new EAuthUserIdentity($eauth);

            // successful authentication
            if ($identity->authenticate()) {
                Yii::app()->user->login($identity);
                //var_dump($identity->id, $identity->name, Yii::app()->user->id);exit;

                // special redirect with closing popup window
                $eauth->redirect();
            }
            else {
                // close popup window and redirect to cancelUrl
                $eauth->cancel();
            }
        }

        // Something went wrong, redirect to login page
        //$this->redirect(array('site/login2'));
        $this->render('login2',  array());
    }
    catch (EAuthException $e) {
        // save authentication error to session
        Yii::app()->user->setFlash('error', 'EAuthException: '.$e->getMessage());

        // close popup window and redirect to cancelUrl
        $eauth->redirect($eauth->getCancelUrl());
    }
}

// default authorization code through login/password ..
$this->render('login2',  array());

问题:

1)$eauth->authenticate()$identity->authenticate()有什么区别?

2) 登录用户设置的“用户”表保存在哪里?

我已经读过这个simular question,这不是我需要的。对不起,如果我问简单的问题,但我真的需要帮助!提前感谢您的任何回复!

【问题讨论】:

    标签: php yii oauth


    【解决方案1】:

    试试

        $serviceName = Yii::$app->getRequest()->getQueryParam('service');
        if (isset($serviceName)) {
            /** @var $eauth \nodge\eauth\ServiceBase */
            $eauth = Yii::$app->get('eauth')->getIdentity($serviceName);
            $eauth->setRedirectUrl(Yii::$app->getUser()->getReturnUrl());
            $eauth->setCancelUrl(Yii::$app->getUrlManager()->createAbsoluteUrl('site/sociallogin'));
    
            try {
                if ($eauth->authenticate()) {
    
                    $identity = User::findByEAuth($eauth);
                    Yii::$app->getUser()->login($identity);
    
                    // special redirect with closing popup window
                    $eauth->redirect();
                }
                else {
                    // close popup window and redirect to cancelUrl
                    $eauth->cancel();
                }
            }
            catch (\nodge\eauth\ErrorException $e) {
                // save error to show it later
                Yii::$app->getSession()->setFlash('error', 'EAuthException: '.$e->getMessage());
    
                $eauth->redirect($eauth->getCancelUrl());
            }
        }
    

    【讨论】:

    • 感谢您的回答,但是,交易在哪里,我不明白身份验证过程是如何工作的
    • 我知道lib的基本知识,但我不知道具体信息,最重要的是:如何设置表名。是否存在任何配置文件、任何常量或一些代码
    【解决方案2】:

    回答您的问题。

    1) $eauth->authenticate() 和 $identity->authenticate() 有什么区别?

    The EAuth extension allows to authenticate users with accounts on other websites.

    这里是demo。我们在使用您的 Facebook 帐户登录时经常看到这种情况。

    支持的协议:OpenID、OAuth 1.0 和 OAuth 2.0。

    EAuth 是一个扩展,用于提供统一的(不依赖于所选服务)方法来验证用户。所以,扩展程序本身不执行登录,不注册用户,不绑定来自不同提供商的用户帐户。

    2) 登录用户设置的“用户”表保存在哪里?

    这当然会因项目而异,因为我的项目当前没有登录用户,但您可以将其添加到您的项目中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-22
      • 2014-06-16
      • 2013-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多