Skip to content

Instantly share code, notes, and snippets.

@schmittjoh
Created June 12, 2011 09:48
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schmittjoh/1021384 to your computer and use it in GitHub Desktop.
Save schmittjoh/1021384 to your computer and use it in GitHub Desktop.
Twitter Anywhere Authentication
<?php
namespace Security\Authentication;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
class AjaxFailureHandler implements AuthenticationFailureHandlerInterface
{
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
return new Response('', 401);
}
}
<?php
namespace Security\Authentication;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
class AjaxSuccessHandler implements AuthenticationSuccessHandlerInterface
{
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
$user = $token->getUser();
return new Response(json_encode(array(
'email' => $user->getEmail(),
'firstname' => $user->getFirstname(),
'lastname' => $user->getLastname(),
)), 200, array('Content-Type', 'application/json'));
}
}
security:
firewalls:
default:
# ...
fos_twitter:
check_path: /twitter_login_check
use_twitter_anywhere: true
provider: twitter
success_handler: ajax_success_handler
failure_handler: ajax_failure_handler
logout:
delete_cookies: [twitter_anywhere_identity]
@ThomasLomas
Copy link

Thank you very much for these code samples. If possible could you please share your provider file and perhaps your user entity? I have been struggling trying to make a Twitter login for a long time and this is really the make or break where I choose Symfony or another framework for the next project.

Thanks a lot :))

@azr
Copy link

azr commented May 30, 2012

Yes that would be great, I have some problems too.

Thanks ! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment