问题
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:
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;
}
回答2:
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');
回答3:
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{
....
}
回答4:
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.
回答5:
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');
}
回答6:
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 toAuth::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
来源:https://stackoverflow.com/questions/19039870/argument-1-passed-to-illuminate-auth-guardlogin-must-implement-interface-ill