我尝试在我的yii应用程序上使用混合身份验证登录facebook,但无法让它工作。所以决定独立工作。我设法让它检索数据并将其存储在我的数据库中。但是,yii,仍然没有检测到用户登录。这是我的 controllers / FacebookController.php
中我的代码的一部分 if (app()->request->isAjaxRequest) {
$user = app()->request->getParam('user');
Shared::debug($user);
// verify one last time that facebook knows this guy
if ($user['id'] === app()->facebook->getUser()) {
if (!empty($user['email']))
{
$model = User::model()->findByEmail($user['email']);
}
else if (!empty($user['username']) && empty($user['email'])) //incase we don't get an email, we use a facebook email
{
$email = $user['username'].'@facebook.com';
$model = User::model()->findByEmail($email);
}
else
{
$model = false;
}
if (!empty($model))
{
// facebook email matches one in the user database
$identity = new UserIdentity( $model->email , null );
$identity->_ssoAuth = true;
$identity->authenticate();
if ($identity->errorCode === UserIdentity::ERROR_NONE) {
print_r($identity);
app()->user->login($identity, null);
echo json_encode(array('error' => false, 'success' => url('/')));
app()->end();
}
else {
echo json_encode(array('error' => 'System Authentication Failed', 'code' => 'auth'));
app()->end();
}
}
在上面的代码中,当我print_r($identity);
时,下面的对象会被回显。和FYI,电子邮件xxxxxx@facebook.com
存储在数据库中,但app()->user->isGuest()
仍然返回true。我在这做错了什么?
UserIdentity Object
(
[_ssoAuth] => 1
[_id:UserIdentity:private] => 19
[username] => xxxxxx@facebook.com
[password] =>
[errorCode] => 0
[errorMessage] =>
[_state:CBaseUserIdentity:private] => Array
(
)
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
答案 0 :(得分:1)
http://yiiframework.com/doc/api/1.1/CWebUser#login-detail
不要将空值传递给app()->user->login($identity, null)
,而是尝试传递像
$duration = 3600*24*30; //30 days
app()->user->login($identity, $duration);
我在将持续时间设置为0时遇到了问题。不确定为什么。但这对我有用了