Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

Return to symfonycasts.com



Subscribe via RSS or know about updates first by getting on the newsletter.

Who we are

Blog > Introducing KnpUOAuth2ClientBundle: Making Social Easy

Introducing KnpUOAuth2ClientBundle: Making Social Easy

By weaverryan | Tech | Feb 23rd 2016 |

Introducing KnpUOAuth2ClientBundle: Making Social Easy

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.

Simple to use: You're in Control

In a nut-shell, here's how it works:

1) You configure a provider. This gives you a "client" service:

# app/config/config.yml
        # will create a service: knpu.oauth2.client.facebook_main
            type: facebook
            client_id: %facebook_app_id%
            client_secret: %facebook_app_secret%
            # see below
            redirect_route: connect_facebook_check

2) 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')

     * Facebook redirects to back here afterwards
     * @Route("/connect/facebook/check", name="connect_facebook_check")
    public function connectCheckAction(Request $request)
        $client = $this->get('oauth2.registry')

        $user = $client->fetchUser();
        // do something with all this new power!

Simple, right?

Guard Auth Integration

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.

Tutorial & Documentation

Ready to try it? Let's do this!

A) Official Documentation

B) Example Project

C) Tutorial (coming soon)

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.