If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.
With a Subscription, click any sentence in the script to jump to that part of the video!
Login SubscribeGo back to the login form. What happens if we fail login? Right now, there are two ways to fail: if we can't find a User
for the email or if the password is incorrect. Let's try a wrong password first.
Enter a real email from the database... and then any password that isn't "tada". And... yep! We hit the dd()
... that comes from onAuthenticationFailure()
:
... lines 1 - 19 | |
class LoginFormAuthenticator extends AbstractAuthenticator | |
{ | |
... lines 22 - 64 | |
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response | |
{ | |
dd('failure'); | |
} | |
... lines 69 - 79 | |
} |
So no matter how we fail authentication, we end up here, and we're passed an $exception
argument. Let's also dump that:
... lines 1 - 19 | |
class LoginFormAuthenticator extends AbstractAuthenticator | |
{ | |
... lines 22 - 64 | |
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response | |
{ | |
dd('failure', $exception); | |
} | |
... lines 69 - 79 | |
} |
Head back... and refresh. Cool! It's a BadCredentialsException
.
This is cool. If authentication fails - no matter how it fails - we're going to end up here with some sort of AuthenticationException
. BadCredentialsException
is a subclass of that.... as is the UserNotFoundException
that we're throwing from our user loader callback:
... lines 1 - 19 | |
class LoginFormAuthenticator extends AbstractAuthenticator | |
{ | |
... lines 22 - 35 | |
public function authenticate(Request $request): PassportInterface | |
{ | |
... lines 38 - 40 | |
return new Passport( | |
new UserBadge($email, function($userIdentifier) { | |
... lines 43 - 45 | |
if (!$user) { | |
throw new UserNotFoundException(); | |
} | |
... lines 49 - 50 | |
}), | |
... lines 52 - 54 | |
); | |
} | |
... lines 57 - 79 | |
} |
All of these exception classes have one important thing in common. Hold Command
or Ctrl
to open up UserNotFoundException
to see it. All of these authentication exceptions have a special getMessageKey()
method that contains a safe explanation of why authentication failed. We can use this to tell the user what went wrong.
So here's the big picture: when authentication fails, it's because something threw an AuthenticationException
or one of its sub-classes. And so, since we're throwing a UserNotFoundException
when an unknown email is entered... if we try to log in with a bad email, that exception should be passed to onAuthenticationFailure()
.
Let's test that theory. At the login form, enter some invented email... and... submit. Oh! We still get a BadCredentialsException
! I was expecting this to be the actual exception that was thrown: the UserNotFoundException
.
For the most part... that is how this works. If you throw an AuthenticationException
during the authenticator process, that exception is passed to you down in onAuthenticationFailure()
. Then you can use it to figure out what went wrong. However, UserNotFoundException
is a special case. On some sites, when the user enters a valid email address but a wrong password, you might not want to tell the user that email was in fact found. So you say "Invalid credentials" both if the email wasn't found or if the password was incorrect.
This problem is called user enumeration: it's where someone can test emails on your login form to figure out which people have accounts and which don't. For some sites, you definitely do not want to expose that information.
And so, to be safe, Symfony converts UserNotFoundException
to a BadCredentialsException
so that entering an invalid email or invalid password both give the same error message. However, if you do want to be able to say "Invalid email" - which is much more helpful to your users - you can do this.
Open up config/packages/security.yaml
. And, anywhere under the root security
key, add a hide_user_not_found
option set to false
:
security: | |
... lines 2 - 4 | |
hide_user_not_found: false | |
... lines 6 - 37 |
This tells Symfony to not convert UserNotFoundException
to a BadCredentialsException
.
If we refresh now... boom! Our UserNotFoundException
is now being passed directly to onAuthenticationFailure()
.
Ok, so let's think. Down in onAuthenticationFailure()
... what do we want to do? Our job in this method is, as you can see, to return a Response
object. For a login form, what we probably want to do is redirect the user back to the login page but show an error.
To be able to do that, let's stash this exception - which holds the error message - into the session. Say $request->getSession()->set()
. We can really use whatever key we want... but there's a standard key that's used to store authentication errors. You can read it from a constant: Security
- the one from the Symfony Security component - ::AUTHENTICATION_ERROR
. Pass $exception
to the second argument:
... lines 1 - 20 | |
class LoginFormAuthenticator extends AbstractAuthenticator | |
{ | |
... lines 23 - 65 | |
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response | |
{ | |
$request->getSession()->set(Security::AUTHENTICATION_ERROR, $exception); | |
... lines 69 - 72 | |
} | |
... lines 74 - 84 | |
} |
Now that the error is in the session, let's redirect back to the login page. I'll cheat and copy the RedirectResponse
from earlier... and change the route to app_login
:
... lines 1 - 20 | |
class LoginFormAuthenticator extends AbstractAuthenticator | |
{ | |
... lines 23 - 65 | |
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response | |
{ | |
$request->getSession()->set(Security::AUTHENTICATION_ERROR, $exception); | |
return new RedirectResponse( | |
$this->router->generate('app_login') | |
); | |
} | |
... lines 74 - 84 | |
} |
Cool! Next, inside login()
controller, we need to read that error and render it. The most straightforward way to do that would be to grab the session and read out this key. But... it's even easier than that! Symfony provides a service that will grab the key from the session automatically. Add a new argument type-hinted with AuthenticationUtils
:
... lines 1 - 7 | |
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils; | |
class SecurityController extends AbstractController | |
{ | |
... lines 12 - 14 | |
public function login(AuthenticationUtils $authenticationUtils): Response | |
{ | |
... lines 17 - 19 | |
} | |
} |
And then give render()
a second argument. Let's pass an error
variable to Twig set to $authenticationUtils->getLastAuthenticationError()
:
... lines 1 - 9 | |
class SecurityController extends AbstractController | |
{ | |
... lines 12 - 14 | |
public function login(AuthenticationUtils $authenticationUtils): Response | |
{ | |
return $this->render('security/login.html.twig', [ | |
'error' => $authenticationUtils->getLastAuthenticationError(), | |
]); | |
} | |
} |
That's just a shortcut to read that key off of the session.
This means that the error
variable is literally going to be an AuthenticationException
object. And remember, to figure out what went wrong, all AuthenticationException
objects have a getMessageKey()
method that returns an explanation.
In templates/security/login.html.twig
, let's render that. Right after the h1
, say if error
, then add a div
with alert alert-danger
. Inside render error.messageKey
:
... lines 1 - 4 | |
{% block body %} | |
<div class="container"> | |
<div class="row"> | |
<div class="login-form bg-light mt-4 p-4"> | |
<form method="post" class="row g-3"> | |
<h1 class="h3 mb-3 font-weight-normal">Please sign in</h1> | |
{% if error %} | |
<div class="alert alert-danger">{{ error.messageKey }}</div> | |
{% endif %} | |
... lines 15 - 29 | |
</form> | |
</div> | |
</div> | |
</div> | |
{% endblock %} |
You don't want to use error.message
because if you had some sort of internal error - like a database connection error - that message might contain sensitive details. But error.messageKey
is guaranteed to be safe.
Testing time! Refresh! Yes! We're redirected back to /login
and we see:
Username could not be found.
That's the message if the User
object can't be loaded: the error that comes form UserNotFoundException
. It's... not a great message... since our users are logging in with an email, not a username.
So next, let's learn how to customize these error messages and add a way to log out.
Hey Mepcuk!
I haven't hit this error, but I noticed it's mentioned in the EasyAdmin docs: https://symfony.com/bundles...
Let me know if that helps - the docs are still a bit cryptic, but I'm not sure exactly why you're seeing this.
Cheers!
I love this new module.
There might be a small misnaming:
Under AuthenticationUtils: Rendering the Error, I guess we should read:
Cool! Next, inside SecurityController, we need to read that error and render it.
instead of:
Cool! Next, inside LoginController, we need to read that error and render it.
Hey Nicolas,
Thank you for this report! I agree, something is not right with that line, but I fixed it in a bit different way, see: https://github.com/SymfonyC... - the problem is that we do want scripts match the video, and Ryan says "login controller" in the video. So, scripts are kinda correct, but meaning is a bit different. Under "login controller" Ryan exactly means that login() action instead of a LoginController as it was highlighted :)
Anyway, now scripts are correct, thank you!
Cheers!
// composer.json
{
"require": {
"php": ">=8.1",
"ext-ctype": "*",
"ext-iconv": "*",
"babdev/pagerfanta-bundle": "^3.3", // v3.3.0
"composer/package-versions-deprecated": "^1.11", // 1.11.99.4
"doctrine/annotations": "^1.0", // 1.13.2
"doctrine/doctrine-bundle": "^2.1", // 2.6.3
"doctrine/doctrine-migrations-bundle": "^3.0", // 3.1.1
"doctrine/orm": "^2.7", // 2.10.1
"knplabs/knp-markdown-bundle": "^1.8", // 1.9.0
"knplabs/knp-time-bundle": "^1.11", // v1.16.1
"pagerfanta/doctrine-orm-adapter": "^3.3", // v3.3.0
"pagerfanta/twig": "^3.3", // v3.3.0
"phpdocumentor/reflection-docblock": "^5.2", // 5.2.2
"scheb/2fa-bundle": "^5.12", // v5.12.1
"scheb/2fa-qr-code": "^5.12", // v5.12.1
"scheb/2fa-totp": "^5.12", // v5.12.1
"sensio/framework-extra-bundle": "^6.0", // v6.2.0
"stof/doctrine-extensions-bundle": "^1.4", // v1.6.0
"symfony/asset": "5.3.*", // v5.3.4
"symfony/console": "5.3.*", // v5.3.7
"symfony/dotenv": "5.3.*", // v5.3.8
"symfony/flex": "^1.3.1", // v1.17.5
"symfony/form": "5.3.*", // v5.3.8
"symfony/framework-bundle": "5.3.*", // v5.3.8
"symfony/monolog-bundle": "^3.0", // v3.7.0
"symfony/property-access": "5.3.*", // v5.3.8
"symfony/property-info": "5.3.*", // v5.3.8
"symfony/rate-limiter": "5.3.*", // v5.3.4
"symfony/runtime": "5.3.*", // v5.3.4
"symfony/security-bundle": "5.3.*", // v5.3.8
"symfony/serializer": "5.3.*", // v5.3.8
"symfony/stopwatch": "5.3.*", // v5.3.4
"symfony/twig-bundle": "5.3.*", // v5.3.4
"symfony/ux-chartjs": "^1.3", // v1.3.0
"symfony/validator": "5.3.*", // v5.3.8
"symfony/webpack-encore-bundle": "^1.7", // v1.12.0
"symfony/yaml": "5.3.*", // v5.3.6
"symfonycasts/verify-email-bundle": "^1.5", // v1.5.0
"twig/extra-bundle": "^2.12|^3.0", // v3.3.3
"twig/string-extra": "^3.3", // v3.3.3
"twig/twig": "^2.12|^3.0" // v3.3.3
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.3", // 3.4.0
"symfony/debug-bundle": "5.3.*", // v5.3.4
"symfony/maker-bundle": "^1.15", // v1.34.0
"symfony/var-dumper": "5.3.*", // v5.3.8
"symfony/web-profiler-bundle": "5.3.*", // v5.3.8
"zenstruck/foundry": "^1.1" // v1.13.3
}
}
Got error - try to redirect to EasyAdmin4