如何使用jwt-auth验证Facebook用户?

时间:2015-06-03 10:04:14

标签: php facebook authentication laravel-5 jwt

我正在使用Laravel 5和jwt-auth软件包来处理JWT。如果用户来自Facebook,我该如何进行身份验证?

我的想法是,我要使用FB ID和电子邮件进行身份验证。我通过将密码更改为fb_id来做到这一点,不幸的是它没有用。任何建议,为什么我会得到以下错误?谢谢!

验证码

    //facebook
    if(Input::has('fb_id')){
        $credentials = Input::only('email','fb_id');
    }else{
        $credentials = Input::only('email', 'password');
    }

    try {
        // attempt to verify the credentials and create a token for the user
        if (! $token = JWTAuth::attempt($credentials)) {
            return Response::json(['error' => 'invalid_credentials'], 401);
        }
    } catch (JWTException $e) {
        // something went wrong whilst attempting to encode the token
        return Response::json(['error' => 'could_not_create_token'], 500);
    }

错误:

ErrorException in EloquentUserProvider.php line 108:
Undefined index: password

1 个答案:

答案 0 :(得分:8)

更新:通过使用JWTAuth :: fromUser($ user)进行管理以使其正常工作。

//find the user using his details.
$user = User::where('email','=',Input::get('email'))->where('fb_token','=',Input::get('fb_id'))->first();

//then use
$token = JWTAuth::fromUser($user);