tl;dr KnpUOAuth2ClientBundle has hit 1.0 - use it for all sorts of "social connect" functionality in Symfony without a headache.
Logging in with Facebook, connecting with your Twitter account, or registering via GitHub - integrating with "Social" networks is now a standard feature of many sites. But in Symfony, it wasn't easy enough to build this. The most popular solution - HWIOAuthBundle - is good and has a lot of built-in OAuth providers. But, it's also confusing to use and frustrating to extend. I know, we use it here on KnpUniversity. I wanted a better option.
Today, I'm thrilled to announce the 1.0 release of KnpUOAuth2ClientBundle, which is built on the backs of the wonderful OAuth 2.0 Client Library from the PHP League. In other words: take the best from the PHP world and make it sing inside of Symfony.
- Easily configure OAuth2 client services
- Integrate with Guard Auth for authentication
- Support for "finish registration" when a brand new user connects to a service
- Automatic support for OAuth "state"
In a nut-shell, here's how it works:
- You configure a provider. This gives you a "client" service:
# app/config/config.yml
knpu_oauth2_client:
clients:
# will create a service: knpu.oauth2.client.facebook_main
facebook_main:
type: facebook
client_id: %facebook_app_id%
client_secret: %facebook_app_secret%
# see below
redirect_route: connect_facebook_check
- Use the new service to redirect to Facebook and do some cool stuff when the user comes back:
// ...
class FacebookController extends Controller
{
/**
* Link to this controller to start the "connect" process
*
* @Route("/connect/facebook")
*/
public function connectAction()
{
return $this->get('oauth2.registry')
->getClient('facebook_main')
->redirect();
}
/**
* Facebook redirects to back here afterwards
*
* @Route("/connect/facebook/check", name="connect_facebook_check")
*/
public function connectCheckAction(Request $request)
{
$client = $this->get('oauth2.registry')
->getClient('facebook_main');
$user = $client->fetchUser();
// do something with all this new power!
$user->getFirstName();
}
}
Simple, right?
You can use this new power to just "get some user information" or actually authenticate your user. For that, the bundles comes with integration for Symfony's Guard security. This includes the ability to "finish registration": i.e. redirect a new user to a registration form before logging them in. See the tutorial below for more info.
Ready to try it? Let's do this!
Like everything, this is a community project meant to make our collective lives easier (and honestly, more fun). If you have some ideas or problems - please open an issue.
Cheers!