SammyK / LaravelFacebookSdk

Fully unit tested Facebook SDK v5 integration for Laravel & Lumen
MIT License
692 stars 200 forks source link

Session issue #82

Closed xadim closed 9 years ago

xadim commented 9 years ago

@SammyK Thanks for the support! I'm having the same problem in Laravel 5. Dumping these 2 blocs : var_dump(\Session::get('facebook.state')); var_dump(\Input::get('state')); exit; Return: NULL string(32) "c33aca7a0a0b99d4fa66b094cfcd3fe1" This is my code: // Generate a login URL Route::get('/facebook/login', function(SammyK\LaravelFacebookSdk\LaravelFacebookSdk $fb) { $login_link = $fb ->getRedirectLoginHelper() ->getLoginUrl('http://my-local-domain.dev/facebook/callback', ['email', 'user_events']);

echo 'Log in with Facebook'; });

// Endpoint that is redirected to after an authentication attempt Route::get('/facebook/callback', function(SammyK\LaravelFacebookSdk\LaravelFacebookSdk $fb) { // Obtain an access token. try { $token = $fb->getAccessTokenFromRedirect(); } catch (Facebook\Exceptions\FacebookSDKException $e) { dd($e->getMessage()); }

// Access token will be null if the user denied the request // or if someone just hit this URL outside of the OAuth flow. 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 2.0 client handler $oauth_client = $fb->getOAuth2Client();

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

}

$fb->setDefaultAccessToken($token);

// Save for later Session::put('fb_user_access_token', (string) $token);

// Get basic info on the user from Facebook. try { $response = $fb->get('/me?fields=id,name,email'); } catch (Facebook\Exceptions\FacebookSDKException $e) { dd($e->getMessage()); }

// Convert the response to a Facebook/GraphNodes/GraphUser collection $facebook_user = $response->getGraphUser();

// Create the user if it does not exist or update the existing entry. // This will only work if you've added the SyncableGraphNodeTrait to your User model. $user = App\User::createOrUpdateGraphNode($facebook_user);

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

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

Any help would really appreciated! Thanks again, xadim.

xadim commented 9 years ago

My Stack is: Apache, PHP, and MySQL on Mac OS X Yosemite

SammyK commented 9 years ago

Hey @xadim! This is a known bug that seems to affect people with MAMP installed the most. I haven't been able to track down why it happens as it seems to be intermittent and only on certain platforms. The latest version of LaravelFacebookSdk doesn't suffer from this issue which is for Laravel 5.x.

Sorry I couldn't help you on this - but if you find the source of the bug, I'd give you a digital hug! :)

ghost commented 9 years ago

Thanks @SammyK for follow up!