2015-10-05 162 views
0

我正在嘗試使用Laravel 5.1登錄facebook。無法通過Facebook在Laravel 5.1登錄

我遵循laravel文檔中提到的每個步驟。 http://laravel.com/docs/5.1/authentication#social-authentication

但是,當我通過Facebook登錄,然後它將重定向到我的正常登錄頁面。 在排序會話是存儲在Facebook登錄。

這是我寫的代碼。

Router.php

Route::get('auth/facebook','Auth\[email protected]'); 
Route::get('auth/facebook/callback','Auth\[email protected]'); 

AuthController.php

public function redirectToProvider() 
{ 
    return Socialite::driver('facebook') 
      ->scopes(['email', 'public_profile']) 
      ->redirect(); 
} 

public function handleProviderCallback() 
{ 
    $user = Socialite::driver('github')->user(); 
    $user = Socialite::driver('github')->user(); 
    // OAuth Two Providers 
    $token = $user->token; 
    // OAuth One Providers 
    $token = $user->token; 
    $tokenSecret = $user->tokenSecret; 
    // All Providers 
    $user->getId(); 
    $user->getNickname(); 
    $user->getName(); 
    $user->getEmail(); 
    $user->getAvatar(); 
} 

Services.php

'facebook' => [ 
    'client_id' => '1625567400000000', 
    'client_secret' => 'secret', 
    'redirect' => 'http://localhost:8000/', 
], 

當i型localhost/8000/auth/facebook它會將我重定向到臉書並要求獲得public_profile,電子郵件等的許可。 它會重定向回localhost/auth/login。

而當我在URL中鍵入localhost:8000/auth/facebook/callback時,它會通過這樣的錯誤;

ClientException在Middleware.php線69:

客戶端錯誤:404

回答

1

對於你的情況,我的客人,您正在使用中間件來檢查用戶是否已經登錄。這可能會導致您重定向到的問題localhost/auth/login

我希望fol降脂代碼可能對你有用

public function handleProviderCallback() 
{   
    //retrieve user's information from facebook 
    $socUser = Socialite::driver('facebook')->user(); 

    //check user already exists in db 
    $user = \App\User::where('email', $socUser->getEmail())->first(); 
    if($user) { 
     // if exist, log user into your application 
     // and redirect to any path you want 
     \Auth::login($user); 
     return redirect()->route('user.index'); 
    } 

    //if not exist, create new user, 
    // log user into your application 
    // and resirect to any path you want 
    $user = new \App\User ; 
    $user->email = $socUser->getEmail(); 
    // ... 
    // ... 
    // ... 
    $user->save(); 
    \Auth::login($user); // login user 
    return redirect()->route('user.index'); // redirect 
} 

注:我沒測試我的代碼,但你應該得到一些想法

瞭解更多信息:http://laravel.com/docs/5.1/authentication

和@mimo提,

Your redirect url in the Services.php file has to be

localhost:8000/auth/facebook/callback

0

在Services.php文件您重定向URL必須是

localhost:8000/auth/facebook/callback 
+0

mimo:在更改services.php文件後出現同樣的錯誤。 –

+0

您不應該手動調用URL。在您將某人發送至Facebook進行身份驗證之前,laravel將存儲會話。當用戶回來時,會話將被使用。出於安全原因,無法再次調用URL。 – mimo

+0

我將facebook APP和Services.php的重定向URL更改爲http:// localhost:8000/auth/facebook/callback。 但我仍然得到相同的錯誤。 –