laravel 传递给 Illuminate\Auth\Guard::login() 的参数 1 必须实现接口 Illuminate\Auth\UserInterface,如果打开则为 null:

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19039870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:38:42  来源:igfitidea点击:

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

authenticationlaravellaravel-4

提问by user1072337

I have a Facebook login I am performing with OAuth2, and then I am using Laravel 4's built in Authentication system to log the user back in on revisit. For the majority of users, I see no issues with what I have, but for one user, he is seeing the following error appear on login:

我有一个使用 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();

Here is the relevant login/authentication code in the controller:

这是控制器中的相关登录/身份验证代码:

$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);

               }

This is my code in the model for the table I am using to keep user information, called "Fans":

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

<?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; 
}

This is very perplexing because this login works for all of the other profiles I have tried. It is only with this friend's profile that it is throwing this error. I will also add, that on login (or retries) it will create multiple entries for this user in the database/table. This authentication system is supposed to be set up to prevent exactly this. Is there anything I'm missing?

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

回答by TheAngelM97

in the create() method in the LoginController you must return the $user that worked for me

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

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

        return $user;
    }

回答by maxim.u

Try refactor your code to this:

尝试将您的代码重构为:

$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');

回答by Andreyco

Simple question and the answer:
Does your Fanclass extend Userclass (or implements UserInterface)?
If not, then the simplest way to fix this is

简单的问题和答案:
您的Fan类是否扩展了User类(或实现了UserInterface)?
如果没有,那么解决这个问题的最简单方法是

class Fan extends User{
    ....
}

回答by Nagibaba

try this. I solved my own.

尝试这个。我自己解决的。

$fan = User:where('id',$socialUser->getId())->first();
if(!$fan){
   **$fan** = User:create([
        'facebook_id' => $socialUser->getId(),
        'email' => $socialUser->getEmail()
   ]);
auth()->login($fan);
return return->to('whereYouLike');
}

回答by Broski

I had the same issue and I managed to resolve it by checking few things:

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

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

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

  • As you can see, null givenmeans that the $useryou want to Auth::login($user)is null. Check if the variable it set.
  • Don't put any var_dump()between the Auth::login and the final redirect(). It may stop the Authentification.
  • 如您所见,null given意味着$user您想要的Auth::login($user)为空。检查它是否设置了变量。
  • 不要var_dump()在 Auth::login 和最终的 redirect() 之间放置任何东西。它可能会停止认证。

Hope it helps

希望能帮助到你

回答by devo

Instead of Auth::login($check);or Auth::login($new);you should pass,

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

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

The Auth::login() function accept the first parameter as UserInterface Object like public function login(UserInterface $user, $remember = false)and in the function body, this will take useridlike this $id = $user->getAuthIdentifier();

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

Read API Doc here.

在此处阅读 API 文档。