Skip to content

Instantly share code, notes, and snippets.

@mishak87
Created May 5, 2012 17:31
Show Gist options
  • Save mishak87/2604208 to your computer and use it in GitHub Desktop.
Save mishak87/2604208 to your computer and use it in GitHub Desktop.
Fix for facebook was not playing along with nette sessions
<?php
namespace UserModule;
use FixFacebook;
class FacebookPresenter extends AbstractSignPresenter
{
public function actionLogin()
{
$this->redirectUrl($this->getFacebook()->getLoginUrl(array(
'scope' => array('email'),
'redirect_uri' => $this->link('//default', array(
'backlink' => $this->backlink,
)),
)));
}
public function actionDefault()
{
if ($this->getParam('error') == 'access_denied' && $this->getParam('error_reason') == 'user_denied') {
$this->flashMessage('You have canceled authentication!', 'error');
$this->redirect('Sign:in');
}
$facebook = $this->getFacebook();
$userId = $facebook->getUser();
if ($userId) {
$attributes = $facebook->api('/me', 'GET');
$attributes['user_id'] = $userId;
$email = $attributes['email'];
$user = $this->database->user->getByEmail($email);
if (!$user) {
$registration = $this->session->getSection('registration');
$registration['email'] = $email;
$registration['service'] = 'Facebook';
$registration['attributes'] = $attributes;
$this->session->close();
$this->redirect('Registration:', array(
'backlink' => $this->backlink,
));
} else {
$this->getUser()->login('Facebook', $attributes);
$this->flashMessage('You have been successfully signed in.');
$this->getApplication()->restoreRequest($this->backlink);
$this->redirect($this->afterSignIn);
}
} else {
$this->flashMessage('Something went wrong with authentication via Facebook. Please <a href="%s" class="facebook">try again</a> or use <a href="%s" class="sign-in">Sign-In</a>.', array(
$this->link('login'),
$this->link('Sign:in'),
));
}
}
/**
* @return FixFacebook
*/
private function getFacebook()
{
$parameters = $this->context->parameters['services']['facebook']['app'];
return new FixFacebook(array(
'appId' => $parameters['id'],
'secret' => $parameters['secret'],
), $this->session);
}
}
<?php
class FixFacebook extends BaseFacebook
{
/**
* @var Nette\Http\Session
*/
private $session;
public function __construct($config, Nette\Http\Session $session)
{
$this->session = $session->getSection('facebook');
$session->start();
parent::__construct($config);
}
protected static $kSupportedKeys =
array('state', 'code', 'access_token', 'user_id');
protected function setPersistentData($key, $value)
{
if (!in_array($key, self::$kSupportedKeys)) {
self::errorLog('Unsupported key passed to setPersistentData.');
return;
}
$this->session[$key] = $value;
}
protected function getPersistentData($key, $default = false)
{
if (!in_array($key, self::$kSupportedKeys)) {
self::errorLog('Unsupported key passed to getPersistentData.');
return $default;
}
return isset($this->session[$key]) ? $this->session[$key] : $default;
}
protected function clearPersistentData($key)
{
if (!in_array($key, self::$kSupportedKeys)) {
self::errorLog('Unsupported key passed to clearPersistentData.');
return;
}
unset($this->session[$key]);
}
protected function clearAllPersistentData()
{
foreach (self::$kSupportedKeys as $key) {
$this->clearPersistentData($key);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment