Laravel 5.2 Socialite Facebook API login redirect error

My Facebook login has suddenly stopped working using the Socialite package with Laravel 5.2. I have had it fully functional for a few days now and it just seems to of dropped off. I am fairly new to Laravel so please have some grace if it is something easy.

I get this error:

"The www.facebook.com page isn't working. www.facebook.com redirected you too many times."

I have tried restarting the local server, running commands like php artisan config:clear . Also tried installing with composer running version "laravel/socialite": "^2.0" . I can't help but think it is something actually in the Facebook Developer Apps API config but everything looks okay!

I will provide my code just in case anyone who has the time to help and can spot any bugs. I used this link as a guide to go off https://blog.damirmiladinov.com/laravel/laravel-5.2-socialite-facebook-login.html#.WK8bGBKLRBw

.env

FACEBOOK_CLIENT_ID=xxxxxxxxx
FACEBOOK_CLIENT_SECRET=xxxxxxxx
FACEBOOK_CALLBACK_URL=http://localhost:8000/auth/facebook/

routes.php

Route::get('auth/facebook',  'AuthFacebookController@redirectToProvider');
Route::get('auth/facebook/callback',  'AuthFacebookController@handleProviderCallback');

FacebookController.php

namespace AppHttpControllersAuth;

use IlluminateHttpRequest;

use AppHttpRequests;
use AppHttpControllersController;
use AppSocialAccountService;
use Socialite;


class FacebookController extends Controller
{

    public function redirectToProvider()
    {

       //send off a request and to FB and get a token
       return Socialite::driver('facebook')->redirect();

    }

    public function handleProviderCallback(SocialAccountService $service)
    {

        $user = $service->createOrGetUser(Socialite::driver('facebook')->user());

        auth()->login($user);

        return redirect()->to('account/{id}/myaccount');
    }
}

SocialAccountService.php

namespace App;

use LaravelSocialiteContractsUser as ProviderUser;

class SocialAccountService
{
    public function createOrGetUser(ProviderUser $providerUser)
    {

        $account = SocialAccount::whereProvider('facebook')
            ->whereProviderUserId($providerUser->getId())
            ->first();

        if ($account) {
            return $account->user;
        } else {

            $account = new SocialAccount([
                'provider_user_id' => $providerUser->getId(),
                'provider' => 'facebook'
            ]);

            $user = User::whereEmail($providerUser->getEmail())->first();

            if (!$user) {

                $user = User::create([
                    'email' => $providerUser->getEmail(),
                    'name' => $providerUser->getName(),
                ]);
            }

            $account->user()->associate($user);
            $account->save();

            return $user;

        }

    }
}

Right guys, if anyone is facing issues like this I can be fairly certain that your routes or callback URL is slightly incorrect, either within your developers.facebook.com account or your .env file config. In my case in my .env I had my callback with an additional trailing '/' .

Causing the redirect issue:

FACEBOOK_CALLBACK_URL=http://localhost:8000/auth/facebook/

Fixed the redirect issue:

FACEBOOK_CALLBACK_URL=http://localhost:8000/auth/facebook

The callback from Facebook appends '/callback?code=xxxxYOUR-TOKENxxxxxx'

So with that extra '/' it seemed to be confusing the callback causing the redirect loop.

链接地址: http://www.djcxy.com/p/5656.html

上一篇: Webhooks没有被解雇。 API nexmo

下一篇: Laravel 5.2 Socialite Facebook API登录重定向错误