传递给Illuminate \ Auth \ Guard :: login()的参数1必须实现接口Illuminate \ Auth \ UserInterface,null给定open:

时间:2013-09-26 22:38:04

标签: authentication laravel laravel-4

我正在使用OAuth2进行Facebook登录,然后我使用Laravel 4内置的身份验证系统重新登录用户。对于大多数用户,我认为没有任何问题,但对于一个用户,他看到登录时出现以下错误:

ErrorException
Argument 1 passed to Illuminate\Auth\Guard::login() must implement interface Illuminate\Auth\UserInterface, null given
open: */nfs/c09/h04/mnt/139243/domains/crowdsets.com/vendor/laravel/framework/src/Illuminate/Auth/Guard.php
*        /**
        * Log a user into the application.
        *
        * @param  \Illuminate\Auth\UserInterface  $user
        * @param  bool  $remember
        * @return void
        */
       public function login(UserInterface $user, $remember = false)
       {
               $id = $user->getAuthIdentifier();

以下是控制器中的相关登录/验证代码:

$check = Fan::where('fbid', '=', $user['uid'])->first();

                if(is_null($check)) {

                  $fan = new Fan;

                  $fan->fbid = $user['uid'];
                  $fan->email = $user['email'];
                  $fan->first_name = $user['first_name'];
                  $fan->last_name = $user['last_name'];
                  $fan->gender = $user['gender'];
                  $fan->birthday = $user['birthday'];
                  $fan->age = $age;
                  $fan->city = $city;
                  $fan->state = $state_abbrev;
                  $fan->image = $user['image'];
                  $fan->friend_ids_url = $user['friends'];
                  $fan->friend_ids_unpacked = $friend_ids;

                  $fan->save();

                  $new = Fan::where('fbid', '=', $user['uid'])->first();

                  Auth::login($new);
                  return Redirect::to('fans/home');

                }

               else {

                  Auth::login($check);

                  $fan = Fan::find(Auth::user()->id);
                  $fan->image = $user['image'];
                  $fan->save();

                  return Redirect::to('fans/home');
                  var_dump($user);

               }

这是我用来保存用户信息的表模型中的代码,称为“粉丝”:

<?php

use Illuminate\Auth\UserInterface;

class Fan extends Eloquent implements UserInterface {
    protected $guarded = array();

    public static $rules = array();

    public function getAuthIdentifier() 
{ 
return $this->getKey(); 
}


public function getAuthPassword() 
{ 
return $this->password; 
}

这非常令人困惑,因为此登录适用于我尝试过的所有其他配置文件。只有这个朋友的个人资料才会抛出这个错误。我还要补充一点,在登录(或重试)时,它将在数据库/表中为该用户创建多个条目。应该设置此身份验证系统以防止这种情况发生。有什么我想念的吗?

6 个答案:

答案 0 :(得分:6)

在LoginController的create()方法中,您必须返回$ user 这对我有用

protected function create(array $data)
    {
        $user = User::create([
            'username' => $data['username'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);

        return $user;
    }

答案 1 :(得分:5)

尝试将代码重构为:

$fan = Fan::where('fbid', '=', $user['uid'])->first();

if (is_null($fan)) {
    $fan = new Fan;

    $fan->fbid = $user['uid'];
    $fan->email = $user['email'];
    $fan->first_name = $user['first_name'];
    $fan->last_name = $user['last_name'];
    $fan->gender = $user['gender'];
    $fan->birthday = $user['birthday'];
    $fan->age = $age;
    $fan->city = $city;
    $fan->state = $state_abbrev;
    $fan->image = $user['image'];
    $fan->friend_ids_url = $user['friends'];
    $fan->friend_ids_unpacked = $friend_ids;

    $fan->save();
} else {
    $fan->image = $user['image'];
    $fan->save();
}

Auth::loginUsingId($fan->getAuthIdentifier());
return Redirect::to('fans/home');

答案 2 :(得分:2)

简单的问题和答案:
您的Fan课程是否延长User课程(或实施UserInterface)?
如果没有,那么最简单的解决方法是

class Fan extends User{
    ....
}

答案 3 :(得分:0)

而不是Auth::login($check);Auth::login($new);,你应该通过,

Auth::login(Auth::user());

Auth :: login()函数接受第一个参数作为UserInterface对象,如public function login(UserInterface $user, $remember = false),在函数体中,这将userid像这样$id = $user->getAuthIdentifier();

阅读API文档here

答案 4 :(得分:0)

试试这个。我解决了自己的问题。

Time Elapsed 00:00:14.68
[BTP] $ powershell.exe -NonInteractive -ExecutionPolicy ByPass "& 'C:\Users\ADMINI~1\AppData\Local\Temp\hudson5229396596966613327.ps1'"
Test-Path : Cannot retrieve the dynamic parameters for the cmdlet. Retrieving the COM class factory for component with 
CLSID {688EEEE5-6A7E-422F-B2E1-6AF00DC944A6} failed due to the following error: 80040154 Class not registered 
(Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
At C:\Release\RPCPS\WebApiPublish-2.ps1:24 char:4
+ if(Test-Path IIS:\Sites\$site)
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Test-Path], ParameterBindingException
    + FullyQualifiedErrorId : GetDynamicParametersException,Microsoft.PowerShell.Commands.TestPathCommand

Test-Path : Cannot retrieve the dynamic parameters for the cmdlet. Retrieving the COM class factory for component with 
CLSID {688EEEE5-6A7E-422F-B2E1-6AF00DC944A6} failed due to the following error: 80040154 Class not registered 
(Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
At C:\Release\RPCPS\WebApiPublish-2.ps1:24 char:4
+ if(Test-Path IIS:\Sites\$site)
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Test-Path], ParameterBindingException
    + FullyQualifiedErrorId : GetDynamicParametersException,Microsoft.PowerShell.Commands.TestPathCommand

答案 5 :(得分:0)

我有同样的问题,我设法通过检查几件事来解决它:

Argument 1 passed to Illuminate\Auth\Guard::login() must implement interface Illuminate\Auth\UserInterface, null given open:

  • 如您所见,null given表示您想要$user的{​​{1}}为空。检查它设置的变量。
  • 不要在Auth :: login和最终重定向()之间放置任何Auth::login($user)。它可能会停止Authentification。

希望有所帮助