Email Login for the Friends of Symfony User Bundle
Recently I have been learning to use Symfony for a project of mine as a less opinionated and more modular alternative to Laravel. So far it has been a very nice experience, and I believe it is a great framework for large scale developments.
Symfony uses Bundles, which are redistributable and standalone packages that provide certain features to your website. One of the most popular ones obviously is a User Bundle to handle Registration, Login, Forgot Password etc.
For this, the Friends of Symfony User Bundle is a great solution which acts as a User provider for the core Security Bundle.
However, there is one major problem I have with it. It uses a Username field instead of just an email field, which while great for things such as community websites, it is quite terrible for Service based websites. Asking someone to call themselves johnsmith22 is embaressing sometimes.
Thankfully, Symfony is designed to be very modular and changeable, so in this post I am going to show you how to setup Email only login and registration with the FOS User Bundle.
You can find the instructions to set up the FOS User Bundle in their documentation, and then follow this on from there
Instructions
First, we have to enable login with either Email or Username which is something that the FOS User Bundle offers as an option.
Next we create an Event Listener, which subscribes to the Registration form submission event and triggers a function to modify the data before it is saved to the database.
In order to hide the Username component of the default form, we have to override it with a new RegistrationFormType
Then we have to let the FOS User Bundle know what form to use
Finally, we need to register the Event Listener and Form Type as Services so that Symfony can use them
Update 18/09/2016
Symfony 3 has made some slight changes so you need to change the following:
- The
fos_user
registration form type inconfig.yml
should now beAppBundle\Form\Type\RegistrationFormType
getParent
inRegistrationFormType.php
should now return'FOS\UserBundle\Form\Type\RegistrationFormType'
- Add
getBlockPrefix()
function toRegistrationFormType.php
:public function getBlockPrefix() { return 'app_user_registration'; }
getName
inRegistrationFormType.php
should now return$this->getBlockPrefix()
After those changes it will work perfectly in Symfony 3
Conclusion
All done, now Users can register using only their email instead of creating a username.
I’ll most likely be doing some more writing on Symfony in the near future, its quite nice and has plenty of features I could write about.
Much of the code for this was sourced from Issue #555, this is a collation of their efforts