0
votes

I'm working with Laravel 5.2 and I'm using SammyK/laravel-facebook-sdk Package i Tried the code in the following link enter link description here

I keep on having this error : "Cross-site request forgery validation failed. The "state" param from the URL and session do not match."

this is my controller code :

public function callbackfbsdk(Facebook $fb){


$helper = $fb::getRedirectLoginHelper();

$loginUrl = $helper->getLoginUrl(['email']);

var_dump($loginUrl);

try {
    $token = $fb::getAccessTokenFromRedirect();
} catch (Facebook\Exceptions\FacebookSDKException $e) {
    dd($e->getMessage());
}

if (! $token) {
    // Get the redirect helper
    $helper = $fb::getRedirectLoginHelper();

    if (! $helper->getError()) {
        abort(403, 'Unauthorized action.');
    }

    // User denied the request
    dd(
        $helper->getError(),
        $helper->getErrorCode(),
        $helper->getErrorReason(),
        $helper->getErrorDescription()
    );
}

if (! $token->isLongLived()) {
    $oauth_client = $fb::getOAuth2Client();

    try {
        $token = $oauth_client->getLongLivedAccessToken($token);
    } catch (Facebook\Exceptions\FacebookSDKException $e) {
        dd($e->getMessage());
    }
}

$fb::setDefaultAccessToken($token);

Session::put('fb_user_access_token', (string) $token);

try {
    $response = $fb::get('/me?fields=id,name,email');
} catch (Facebook\Exceptions\FacebookSDKException $e) {
    dd($e->getMessage());
}

$facebook_user = $response->getGraphUser();

$user = App\User::createOrUpdateGraphNode($facebook_user);

// Log the user into Laravel
Auth::login($user);

return redirect('/')->with('message', 'Successfully logged in with Facebook');

}

Any Ideas about how can I solve this error??

1

1 Answers

0
votes

why you are putting these line in callback

$helper = $fb::getRedirectLoginHelper();

$loginUrl = $helper->getLoginUrl(['email']);

it will try to generate link again after redirect.seperate it, will solve the problem