I'm using the login form from Symfony, but I can't login, if the entered username is 'FOO' and in the DB is stored 'foo'. I'm using Postgres. It means the username-field is case sensitive. What can I do?
Asked
Active
Viewed 903 times
1
-
If you are in control over what column type is used, maybe you can solve it this way: https://stackoverflow.com/a/4482278/10955263 – 04FS May 17 '19 at 09:16
-
1@olek07 I think in your case you need to update your UserProvider, check https://symfony.com/doc/current/security/user_provider.html, think about updating `loadUserByUsername()` method to remove the sensitive check for your username value – Mohamed Radhi Guennichi May 17 '19 at 09:17
-
Yes, It works. Thank's a lot. – olek07 May 17 '19 at 09:27
1 Answers
5
This depends mainly on your Database-Server. MySQL is case insensitive, PostgreSQL is case sensitive.
But you can write query Like this
$this->createQueryBuilder('user')
->where('LOWER(user.username) = :username')
->setParameter('username', strtolower($username))
->getQuery()
->getResult()
;
Nairi Abgaryan
- 616
- 5
- 16
-
-
-
2Better to store users with noramlized (lower, etc..) usernames to rid of `LOWER` function and reduce DB load. Look how it is made in FOSUserBundle. – Max P. May 17 '19 at 10:59
-
You are right, but users often enter their email-address like Otto.Mustermann@Gmail.com – olek07 May 18 '19 at 07:33