PHP (Symfony)

Community maintained

Sample Project

Download a sample project specific to this tutorial configured with your Auth0 API Keys.

System Requirements
  • PHP 5.3.9
  • Symfony 3.*
Show requirements

If you have used Symfony before, you are probably already familiar with the HWIOAuth Bundle. We'll be using it to integrate a Symfony WebApp with Auth0 and achieve Single Sign On with a few simple steps.

Add HWIOAuthBundle to composer.json

// composer.json

"require": {
    "hwi/oauth-bundle": "^0.5.3",

and run composer update

This sample uses Composer, a tool for dependency management in PHP. It allows you to declare the dependent libraries your project needs and it will install them in your project for you.

Enable the Bundle

// app/AppKernel.php

public function registerBundles()
    $bundles = array(
        // ...
        new HWI\Bundle\OAuthBundle\HWIOAuthBundle(),

Configure the Routes

Add the following routes at the beginning of app/config/routing.yml

    resource: "@HWIOAuthBundle/Resources/config/routing/redirect.xml"
    prefix:   /connect

    resource: "@HWIOAuthBundle/Resources/config/routing/login.xml"
    prefix:   /login

    path:    /auth0/callback

    path: /auth0/logout

Configure Auth0

For security purposes, you must add the callback URL of your app to your Client Settings.

Your callback URL is currently set to:


In this case, the callbackURL should look something like:


Configure the Resource Owner

Add this to your app/config/config.yml

    firewall_names: [secured_area]
            type:                auth0
            base_url:            https://YOUR_AUTH0_DOMAIN
            client_id:           YOUR_CLIENT_ID
            client_secret:       YOUR_CLIENT_SECRET
            scope: "openid profile"

User Provider

You can create a user provider that implements OAuthAwareUserProviderInterface and set it up in the next step, or you can use one of the predefined services that HWIOAuthBundle provides.

Configure the OAuth Firewall

This is where you set the filters to select which pages are protected (aka, needs login). You can read more on how to configure this at the Symfony security docs.

This is a basic example that allows anonymous users and then restricts access to the /demo/hello/ route. It doesn't store the users in a DB.

This file is app/config/security.yml:

            id: hwi_oauth.user.provider

            anonymous: ~
                    auth0: "/auth0/callback"
                login_path:        /login
                use_forward:       false
                failure_path:      /login

                    service: hwi_oauth.user.provider
                path:   /auth0/logout
                target: /

        - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/secured, roles: ROLE_OAUTH_USER }

Notice that we need to identify the user provided selected in the step before both in the providers and in the firewall.

Triggering Login and accessing user information

Set the following in app/resources/views/index.html.twig

{% if app.user %}
    Welcome, {{ app.user.username }}!<br/>
    {{ dump(app.user) }}
    <a href="{{ logout_url("secured_area") }}">
{% else %}
    <h1>Symfony Auth0 Quickstart</h1>
    <script src=""></script>
    <script type="text/javascript">
        var webAuth = new auth0.WebAuth({
            domain: 'YOUR_AUTH0_DOMAIN',
            clientID: 'YOUR_CLIENT_ID',
            redirectUri: 'http://localhost:8000/auth0/callback',
            audience: `https://YOUR_AUTH0_DOMAIN/userinfo`,
            responseType: 'code',
            scope: 'openid profile'

        function signin() {
    <button onclick="window.signin();">Login</button>
{% endif %}


SSL certificate problem: self signed certificate in certificate chain

If there is an issue with CAs database on your computer, you may need to download this CAs database. To use it on Windows for example, place it in c:\cacert.pem and point to it in php.ini with openssl.cafile=c:/cacert.pem.

Use Auth0 for FREECreate free Account