PROBLEM: It’s easily possible to log in via username (and password) or to log in via email (and password) but how to enable both for an optimal user experience?
APPROACHES: I found multiple answers on Stackoverflow* and various tutorials, none worked for my Symfony version (6.3.3) or I was just to stupid to make them work (professional web developer).
- Create my own Extension of the AbstractAuthenticator. Failed because: a) Requires a lot of logic to be duplicated, modified. b) Feels like a security risk to play around with the complex login logic functionality too much. c) I failed to get that one important constructor service to work in my extension.
- Alternatively, I could have extended some internal Symfony classes, but they were final, so extending them was not a valid option.
- It’s possible to do manual logins, but that just moves the above problem somewhere else (constructing "passwords", "badges", "tokens", etc.).
SOLUTION: I actually found an elegant workaround. The reason why I’m still asking this question is
a) to share my solution with others and
b) maybe there’s something wrong with it that I’m not seeing it and there’s still a far better way of doing it?
*Similar questions, but with old versions of Symfony (and they didn’t work for me):
2
Answers
So, my solution is:
Instead of doing the following:
I included another action between step 1 and step 2:
1.5 redirects to my own Controller Action, not touching the Symfony login logic: Controller/AuthenticationController.php
Step 3 remains like before:
if you are using symfony maker auth, just edit the authenticate method in the se src/Security/
then you need to change the template for the login form in templates/security/login.html.twig
change the the default input for the email to be