Laravel 4 Auth with Facebook (no password authentication)

I am trying to set up an authentication system with Laravel 4 using Facebook login. I am using the madewithlove / laravel-oauth2 package for Laravel 4.

Of course, there is no password to add to my database when a user logs in with Facebook. However, I am trying to check if there is a user ID in the database to determine if I should create a new object or just enter the current one. I would like to use Auth commands for this. I have a table called "fans."

This is what I work with:

$fan = Fan::where('fbid', '=', $user['uid']); 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; $fan->image = $user['image']; $fan->save(); return Redirect::to('fans/home'); } else { Auth::login($fan); return Redirect::to('fans/home'); } 

Fan Model:

 <?php class Fan extends Eloquent { protected $guarded = array(); public static $rules = array(); } 

When I ran this, I get an error message:

 Argument 1 passed to Illuminate\Auth\Guard::login() must be an instance of Illuminate\Auth\UserInterface, instance of Illuminate\Database\Eloquent\Builder given 

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

I get an error message:

 Argument 1 passed to Illuminate\Auth\Guard::login() must be an instance of Illuminate\Auth\UserInterface, null given, called in /Applications/MAMP/htdocs/crowdsets/laravel-master/vendor/laravel/framework/src/Illuminate/Auth/Guard.php on line 368 and defined 

I do not know why he gives me this error. Do you have any suggestions on how I can do this? Thanks for the help.

+2
source share
2 answers

You need to embed UserInterface in your model for Auth to work properly.

 use Illuminate\Auth\UserInterface; class Fan extends Eloquent implements UserInterface{ ... public function getAuthIdentifier() { return $this->getKey(); } /** * Get the password for the user. * * @return string */ public function getAuthPassword() { return $this->password; } } 

getAuthIdentifier and getAuthPassword are an abstract method and should be implemented in your class that implements UserInterface

+4
source

To log any user into the system, you need to use the User model, and I bet that the inherited classes will do the trick, but I'm not sure.

Anyway, your Fan model has nothing to do with the User model / table and that is the problem. If your model had an belong_to or has_one and a user_id field, you could replace Auth::login($user) with Auth::loginUsingId(<some id>) .


Original answer:

You are missing an additional method call: ->get() or ->first() to actually get the results:

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

Alternatively, you can throw an exception to find out what happens:

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

If you see different errors, update your question with these errors.

0
source

Source: https://habr.com/ru/post/1483159/


All Articles