Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 530
user1072337 Avatar asked Sep 26 '13 22:09

user1072337


2 Answers

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

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

        return $user;
    }
like image 136
Angel Miladinov Avatar answered Oct 03 '22 07:10

Angel Miladinov


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');
like image 34
maxim.u Avatar answered Oct 03 '22 05:10

maxim.u