Docs

PHP (Symfony)

View on Github

PHP (Symfony)

Group 7 Copy 8

This tutorial demonstrates how to add user login to a Symfony application. We recommend you to Log in to follow this quickstart with examples configured for your account.

I want to explore a sample app

2 minutes

Get a sample configured with your account settings or check it out on Github.

View on Github
System requirements: PHP 5.6, 7.0 | Symfony 3.3.*

New to Auth? Learn How Auth0 works, how it integrates with Regular Web Applications and which protocol it uses.

How it works

Configure Auth0

Access Tokens

Get Your Application Keys

When you signed up for Auth0, a new application was created for you, or you could have created a new one.

You will need some details about that application to communicate with Auth0. You can get these details from the Application Settings section in the Auth0 dashboard.

You need the following information:

  • Domain
  • Client ID
  • Client Secret

If you download the sample from the top of this page these details are filled out for you.

If you have more than one application in your account, the sample comes with the values for your Default App.

App Dashboard

ID Tokens

Configure Callback URLs

A callback URL is a URL in your application where Auth0 redirects the user after they have authenticated.

The callback URL for your app must be whitelisted in the Allowed Callback URLs field in your Application Settings. If this field is not set, users will be unable to log in to the application and will get an error.

If you are following along with the sample project you downloaded from the top of this page, the callback URL you need to whitelist in the Allowed Callback URLs field is http://localhost:3000/auth0/callback.

Claims

Configure Logout URLs

A logout URL is a URL in your application that Auth0 can return to after the user has been logged out of the authorization server. This is specified in the returnTo query parameter.

The logout URL for your app must be whitelisted in the Allowed Logout URLs field in your Application Settings. If this field is not set, users will be unable to log out from the application and will get an error.

If you are following along with the sample project you downloaded from the top of this page, the logout URL you need to whitelist in the Allowed Logout URLs field is http://localhost:3000.

Keep reading

Configure Symfony to Use Auth0

Using HWIOAuthBundle for Authentication

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

Add HWIOAuthBundle to composer.json.

and run composer update.

This sample is using curl-client as PHP HTTP client implementation for httplug-bundle, you can use the PHP HTTP client implementation you want.

Enable the Bundle

Configure the Routes

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

Create an Auth0 Resource Owner

You need to create an Auth0 resource owner to enable HWIOAuthBundle to connect to Auth0.

Add this to your src/AppBundle/Auth0ResourceOwner.php

Configure the Resource Owner

Add this to your app/config/config.yml

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 require authentication or authorization. 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 /secured route. It doesn't store the users in a DB.

This file is app/config/security.yml:

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

Trigger Authentication

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

Logout

In your app/config/services.yml add register the logout listener.

Then in your src/listener/LogoutListener.php define the LogoutListener class to handle the logout event.

Use Auth0 for FREE