Laravel 4 Auth with Facebook (no password authentication)

人盡茶涼 提交于 2019-12-20 02:57:06

问题


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

Of course, there is no password to add to my database upon a user loggin in with Facebook. I am, however, trying to check to see if a user id is in the database already to determine if I should create a new entity, or just log in the current one. I would like to use the Auth commands to do this. I have a table called "fans".

This is what I'm working 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 run this, I get the error:

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 the error:

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 it is giving me this error. Do you have suggestions on how I can make this work? Thank you for your help.


回答1:


You have to implement UserInterface to 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 abstract method and must be implemented in you class implementing UserInterface




回答2:


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

Anyway, your Fan model does not associate with the User model/table in any way and that's a problem. If your model had a belong_to or has_one relationship and a user_id field then you could replace Auth::login($user) with Auth::loginUsingId(<some id>).


Original answer:

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

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

Alternatively, you can throw an exception to see what's going on:

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

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



来源:https://stackoverflow.com/questions/17418085/laravel-4-auth-with-facebook-no-password-authentication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!