如何使用脸书,谷歌的身份验证信息.在一


How to use authentication info of facebook, google... in Yii

我的网站使用默认的Yii用户模型:user->id,user->username ...几乎基于用户帐户在网站上激活。

然后,我包括通过谷歌,脸书登录...通过使用 Eauth 扩展。此扩展现在运行良好。我可以从他们那里获得用户名,用户ID。

问题是我的网站只使用Yii的用户帐户来控制进程,所以Google的帐户不能做任何事情。

如何将 Yii 的用户帐户与 Google 或 Facebook 用户帐户同步?

确实很容易。

首先,您必须为用户创建一个表/模型(或使用已有的表/模型)。

然后,在从OAuth回调的操作(我的称为SiteController/actionLoginGoogleDo)上,你必须挂接到标准的Yii用户管理。我写了这几行代码,一切都像魔术一样工作(登录/注销/Yii::app()->user/ecc):

public function actionLoginGoogleDo()
{
    // Replace the next line with your Eauth extension
    $email = Yii::app()->googlePlus->getDetails();
    // Here fetch your user model.
    // In my case if a user is not found, a new one is created.
    $user = User::getUser($email);
    // This is the actual hook in the Yii framework
    $identity=new UserIdentity($email, null);
    $duration; // days
    Yii::app()->user->login($identity,$duration);
    // Redirect to the main controller/action
    $this->redirect(array('site/index'));
}