2015-06-03 41 views
1

我使用Laravel 5和jwt-auth包來處理JWT。如果用戶來自Facebook,我如何進行身份驗證?如何使用jwt-auth認證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 
+0

你可以提供EloquentUserProvider.php與pastebin嗎? 看來你正在使用$密碼,但該變量沒有定義。 – ChainList

+0

@ChainList yup我沒有提供密碼參數,因爲用戶來自Facebook。所以我只有fb_id和電子郵件來驗證用戶。 – bwaaaaaa

回答

8

更新:管理,使其通過JWTAuth :: FROMUSER($用戶)工作。

//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);