This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

CloudwaysCDN — a powerful solution that offers superior performance and satisfied global audience for your business. Read More

Create Token Based API Authentication in Symfony

Updated on  18th July

4 Min Read
Reading Time: 4 minutes

In the prior installment of this series, I wrote about creating a REST API in Symfony.. I used HTTP codes with API responses and threw exceptions on bad response code.

Now what if you want to apply token based authentication system in Symfony and authenticate users through an API key. Symfony provides a very easy solution in the form of Guard authentication bundle. This bundle works with API keys and implements methods to handle the authentication responses and user credentials.

symfony api token authentication

In this article, I will show you how you can work with Guard and authenticate users via API token(s). For the purpose of this article, I am assuming that you have already launched a PHP stack server and application on Cloudways, which is widely known for its Best PHP Hosting. For help on this prerequisite, check out this guide on installing Symfony on Cloudways.

Create a User Class & Provider

To authenticate a user, I need to create a user entity class which implements UserInterface and a user provider. Symfony auth process depends on the UserProvider. When the user hits the submit button, the user provider values are checked. After this, further verification of password takes place.

Following is the entity code:

php hosting signup

Register the Provider in security.yml

The next step is to register the above made user provider in security.yml file, to do that add the following code:

I have done it here now before creating an authenticator class let’s install Guard first.

Symfony Guard Component

Guard authentication first introduced in symfony 2.8 and after that it’s now become a part of symfony core. Guard provides different layers of authentication. With Guard, every step of the authentication process is handled by only one class: an Authenticator. This class will have to implement the provided GuardAuthenticatorInterface.

But still Composer is the most preferred way to install Guard in Symfony. Install with the following command:

Creating An Authenticator Class

Now an authenticator class is needed which implements the GuardAuthenticatorInterface and extends the AbstractGuardAuthenticator. This class will read the api token in header request and find the respective user. Create a new file: src/AppBundle/Security/TokenAuthenticator.php

The Authentication methods are explained with comments but If you want to learn more about Guard authentication method you can learn on symfony documentation page.

Configuring The Authenticator

To configure the authenticator I need to update the firewall:

After that register the authenticator as a service in service.yml:

That’s it finally everything is done now to check the response you can use curl to request it.                                              

You will be authenticated and redirected to the homepage.

You might also like: How To Implement User Authentication In Symfony Using Auth0

Final Words

Guard allows to create custom and simple authentication system which help you to move out from pain of complex authentications. So in this article I have described you how to create a token based authentication system in symfony using Guard component.

If you have any questions or queries you can comment below.

Share your opinion in the comment section. COMMENT NOW

Shahroze Nawaz

Shahroze is a PHP Community Manager at Cloudways - A Managed PHP Hosting Platform. He’s always in search of new frameworks and methods to implement them. Besides his coding life, he loves movies and playing soccer with friends. You can email him at

Create PHP websites without the worry of server management.

Deploy your PHP app on optimized PHP hosting servers.

Get Our Newsletter
Be the first to get the latest updates and tutorials.