11

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:

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?

1
  • Did you get it resolved? if yes, will you please share your thoughts? Jun 12, 2017 at 7:00

6 Answers 6

10

in the create() method in the LoginController you must return the $user

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

        return $user;
    }
5

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

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

class Fan extends User{
    ....
}
2
  • as you can see above I have it implement UserInterface per this line: use Illuminate\Auth\UserInterface; class Fan extends Eloquent implements UserInterface { Sep 27, 2013 at 16:49
  • I also already have the Fan class extending Eloquent...any other ideas? Sep 27, 2013 at 20:35
0

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

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 userid like this $id = $user->getAuthIdentifier();

Read API Doc here.

2
  • When I try this, I get the following error on login: Argument 1 passed to Illuminate\Auth\Guard::login() must implement interface Illuminate\Auth\UserInterface, null given Sep 27, 2013 at 17:01
  • As I said before, using the $check and $new values is working for the majority of users. I have only recently seen this error with 1 user. Sep 27, 2013 at 17:02
0

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');
}
0

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:

  • As you can see, null given means that the $user you 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.

Hope it helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.