Skip to content

Commit 5f9fead

Browse files
committed
Throwing CustomUserMessageAuthenticationException in make:auth login
form The idea is that this is easier and more self-documenting than teaching the user to translate the "Username could not be found." message.
1 parent 14d2946 commit 5f9fead

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

src/Maker/MakeAuthenticator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Bundle\MakerBundle\Security\InteractiveSecurityHelper;
2222
use Symfony\Bundle\MakerBundle\Security\SecurityConfigUpdater;
2323
use Symfony\Bundle\MakerBundle\Security\SecurityControllerBuilder;
24+
use Symfony\Bundle\MakerBundle\Str;
2425
use Symfony\Bundle\MakerBundle\Util\ClassSourceManipulator;
2526
use Symfony\Bundle\MakerBundle\Util\YamlManipulationFailedException;
2627
use Symfony\Bundle\MakerBundle\Util\YamlSourceManipulator;
@@ -237,6 +238,7 @@ private function generateAuthenticatorClass(array $securityData, string $authent
237238
'user_fully_qualified_class_name' => trim($userClassNameDetails->getFullName(), '\\'),
238239
'user_class_name' => $userClassNameDetails->getShortName(),
239240
'username_field' => $userNameField,
241+
'username_field_label' => Str::asHumanWords($userNameField),
240242
'user_needs_encoder' => $this->userClassHasEncoder($securityData, $userClass),
241243
'user_is_entity' => $this->doctrineHelper->isClassAMappedEntity($userClass),
242244
]
@@ -281,7 +283,7 @@ private function generateFormLoginFiles(string $controllerClass, string $userNam
281283
[
282284
'username_field' => $userNameField,
283285
'username_is_email' => false !== stripos($userNameField, 'email'),
284-
'username_label' => ucfirst(implode(' ', preg_split('/(?=[A-Z])/', $userNameField))),
286+
'username_label' => ucfirst(Str::asHumanWords($userNameField)),
285287
]
286288
);
287289
}

src/Resources/skeleton/authenticator/LoginFormAuthenticator.tpl.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Symfony\Component\HttpFoundation\Request;
99
use Symfony\Component\Routing\RouterInterface;
1010
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
11+
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
1112
<?= $user_needs_encoder ? "use Symfony\\Component\\Security\\Core\\Encoder\\UserPasswordEncoderInterface;\n" : null ?>
1213
use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
1314
use Symfony\Component\Security\Core\Security;
@@ -63,10 +64,17 @@ public function getUser($credentials, UserProviderInterface $userProvider)
6364
throw new InvalidCsrfTokenException();
6465
}
6566

66-
<?= $user_is_entity ? "return \$this->entityManager->getRepository($user_class_name::class)->findOneBy(['$username_field' => \$credentials['$username_field']]);\n"
67+
<?= $user_is_entity ? "\$user = \$this->entityManager->getRepository($user_class_name::class)->findOneBy(['$username_field' => \$credentials['$username_field']]);\n"
6768
: "// Load / create our user however you need.
6869
// You can do this by calling the user provider, or with custom logic here.
69-
return \$userProvider->loadUserByUsername(\$credentials['$username_field']);\n"; ?>
70+
\$user = \$userProvider->loadUserByUsername(\$credentials['$username_field']);\n"; ?>
71+
72+
if (!$user) {
73+
// fail authentication with a custom error
74+
throw new CustomUserMessageAuthenticationException('<?= ucfirst($username_field_label) ?> could not be found.');
75+
}
76+
77+
return $user;
7078
}
7179

7280
public function checkCredentials($credentials, UserInterface $user)

src/Str.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,9 @@ public static function areClassesAlphabetical(string $class1, string $class2)
205205

206206
return $arr1[0] == $arr2[0];
207207
}
208+
209+
public static function asHumanWords(string $variableName): string
210+
{
211+
return implode(' ', preg_split('/(?=[A-Z])/', $variableName));
212+
}
208213
}

0 commit comments

Comments
 (0)