# Authentication plugin - Make username case insensitive

**URL:** <https://discourse.cakephp.org/t/authentication-plugin-make-username-case-insensitive/9541>\
**Category:** Plugins\
**Created:** [July 21, 2021, 4:30pm UTC](https://discourse.cakephp.org/t/authentication-plugin-make-username-case-insensitive/9541 "2021-07-21T16:30:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanG](https://avatars.discourse-cdn.com/v4/letter/d/ba9def/32.png) [@DanG](https://discourse.cakephp.org/u/DanG)\
**Post date:** [July 21, 2021, 4:30pm UTC](https://discourse.cakephp.org/t/authentication-plugin-make-username-case-insensitive/9541/1 "2021-07-21T16:30:54Z")

</div>

Hi,

I am trying to figure out how to make the Authentication username be case insensitive. The database (PostGreSQL) we are backing against is case sensitive in it’s varchar fields.

Since we are using email addresses, I am saving them as all lowercase. However, we have some users who insist on punching in their email addresses camel case. Is there a straightforward way to make $this-\>Auth-\>identify() be case insensitive on the username/email address?

Thanks!

---

<div class="post-metadata">

**Author:** ![dreamingmind](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dreamingmind/32/1857_2.png) [@dreamingmind](https://discourse.cakephp.org/u/dreamingmind)\
**Post date:** [July 21, 2021, 5:06pm UTC](https://discourse.cakephp.org/t/authentication-plugin-make-username-case-insensitive/9541/2 "2021-07-21T17:06:44Z")

</div>

You should be able to write your own [Authenticator](https://book.cakephp.org/authentication/2/en/authenticators.html) ?

Then in `Application.php` `getAuthenticationService()` put it into use:

 ![Screen Shot 2021-07-21 at 10.00.26 AM](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/4/4891ce28b4a533a35f0f02aa5211bff75eacaa25.png)

Use your new one instead of `Form`.

As an alternative:

Could you capture the posted data in the `login` method and force it to lowercase before authenticating, and also capture the registration post data and force it lower also before saving?

---

<div class="post-metadata">

**Author:** ![DanG](https://avatars.discourse-cdn.com/v4/letter/d/ba9def/32.png) [@DanG](https://discourse.cakephp.org/u/DanG)\
**Post date:** [July 21, 2021, 5:21pm UTC](https://discourse.cakephp.org/t/authentication-plugin-make-username-case-insensitive/9541/3 "2021-07-21T17:21:28Z")

</div>

Honestly, I would prefer to avoid writing my own authenticator for something this basic. I have written an LDAP authenticator.

The password identifier and the form authenticator do 99% of what I need.

Since the request data is now a protected member in Cake 4.X and the identify() function is literally using the request directly, capturing and modifying before hand is not really an option.

I guess I could always use javascript to lowercase the form field contents on submit. This just seems like such a hack for something that should be built in as an option.

---

<div class="post-metadata">

**Author:** ![dreamingmind](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dreamingmind/32/1857_2.png) [@dreamingmind](https://discourse.cakephp.org/u/dreamingmind)\
**Post date:** [July 21, 2021, 6:06pm UTC](https://discourse.cakephp.org/t/authentication-plugin-make-username-case-insensitive/9541/4 "2021-07-21T18:06:14Z")

</div>

You can insert new data into the request using `Request::withData()`.

```php
$request = $request->withData($key, $value);

```

also, if the password identifier and form authenticator do 99% of what you need then new class would likely be a very simple `extends` of the existing class. Not so hard to write?

---

<div class="post-metadata">

**Author:** ![ryanolton](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/ryanolton/32/1465_2.png) [@ryanolton](https://discourse.cakephp.org/u/ryanolton)\
**Post date:** [July 28, 2021, 3:16pm UTC](https://discourse.cakephp.org/t/authentication-plugin-make-username-case-insensitive/9541/5 "2021-07-28T15:16:08Z")

</div>

I did something like the following using CakePHP 4.x (and the Authentication plugin) having the same Postgres case-sensitivity _issue_ you noted:

In src/Application.php, you should have `getAuthenticationService()` function already in place (as needed by the Authentication plugin), so you should just need to update the way the code Identifies users (or whatever you call your “users”) by using a custom Resolver.

```auto
public function getAuthenticationService( ServerRequestInterface $request ): AuthenticationServiceInterface {
	$service = new AuthenticationService();

	...

	$fields = [
		'username' => 'email',
		'password' => 'password'
	];

	$service->loadIdentifier( 'Authentication.Password', [
		'fields' => $fields,
		'resolver' => [
			'className' => 'Custom'
		]
	] );

	...

	return $service;
}

```

**Note:** the “Custom” name above can be anything you’d like; make sure the Resolver file and class names below matches the name you select.

Also, the fields above are the ones I used, but you can use any names you’d like for username and password; they should match your database field names. Similarly, the fields found in the Resolver `find()` query below should match your database field names.

in src/Identifier/Resolver/CustomResolver.php

```auto
namespace App\Identifier\Resolver;

use Authentication\Identifier\Resolver\ResolverInterface;
use Cake\Core\InstanceConfigTrait;
use Cake\ORM\Locator\LocatorAwareTrait;

class CustomResolver implements ResolverInterface
{
	use InstanceConfigTrait;
	use LocatorAwareTrait;

	protected $_defaultConfig = [];

	public function __construct( array $config = [] ) {
		$this->setConfig( $config );
	}

	public function find( array $conditions, $type = self::TYPE_AND ) {
		return $this->getTableLocator()
			->get( 'Users' )
			->find()
			->where( [
				'lower(Users.email)' => strtolower( $conditions['email'] )
			] )
			->first();
	}
}

```

**Note:** my example uses a `Users` table, but your could be using another table.

You can read more here:

- [Identifiers - 2.x](https://book.cakephp.org/authentication/2/en/identifiers.html)
- [authentication/OrmResolver.php at master · cakephp/authentication · GitHub](https://github.com/cakephp/authentication/blob/master/src/Identifier/Resolver/OrmResolver.php)

---

<div class="post-metadata">

**Author:** ![DanG](https://avatars.discourse-cdn.com/v4/letter/d/ba9def/32.png) [@DanG](https://discourse.cakephp.org/u/DanG)\
**Post date:** [July 28, 2021, 4:18pm UTC](https://discourse.cakephp.org/t/authentication-plugin-make-username-case-insensitive/9541/6 "2021-07-28T16:18:10Z")

</div>

Hot damn. That’s what I am talking about.

The “issue” of being case-sensitive is not really an issue but a feature. They really should provide an option/setting to tell it to make the username case insensitive.

---

<div class="post-metadata">

**Author:** ![KHBOHM](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/khbohm/32/1410_2.png) [@KHBOHM](https://discourse.cakephp.org/u/KHBOHM)\
**Post date:** [June 21, 2022, 9:14am UTC](https://discourse.cakephp.org/t/authentication-plugin-make-username-case-insensitive/9541/7 "2022-06-21T09:14:20Z")

</div>

Muchas gracias ryanolton, esto funcionó perfectamente para mi.

---

<div class="post-metadata">

**Author:** ![midnightcheese](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/midnightcheese/32/2550_2.png) [@midnightcheese](https://discourse.cakephp.org/u/midnightcheese)\
**Post date:** [May 20, 2023, 4:32pm UTC](https://discourse.cakephp.org/t/authentication-plugin-make-username-case-insensitive/9541/8 "2023-05-20T16:32:13Z")

</div>

This thread was very helpful, thank you. For those that do have the option to modify the DB, it is possible to modify the relevant column to be case insensitive. It requires enabling the CITEXT extension and then modifying the column’s data type to CITEXT. [This guide](https://bambielli.com/til/2016-12-28-postgres-extensions-citext/) set me in the right direction.
