如何使用Laravel 5.5身份验证使电子邮件登录大小写不敏感 [英] How to make email login case insensitive with Laravel 5.5 authentication

查看:58
本文介绍了如何使用Laravel 5.5身份验证使电子邮件登录大小写不敏感的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在构建应用程序时,我通过运行 php artisan make:auth 使用Laravel身份验证支架,这很棒,并且节省了很多时间.

When building my application, I used the Laravel authentication scaffolding by running php artisan make:auth which was great and saved me a lot of time.

但是,我遇到了一个问题,用户无法登录,因为他们不记得最初注册时使用的电子邮件大小写.

However, I am running into an issue with users not being able to login because they can't remember what case they used for their email when they originally signed up.

例如,使用 Myemail@domain.com 注册的用户无法使用 myemail@domain.com 登录.

For example, a user that signed up with Myemail@domain.com can not login with myemail@domain.com.

从我的研究中我了解到,在技术上 @之前的部分通过规范,并且从理论上讲,以上两封电子邮件可以属于两个不同的人.但就我的情况而言,我不认为有足够的可能性在登录时强制执行此操作.

I understand from my research that technically the part prior to the @ is case sensitive by virtue of the spec, and that the two emails above could, in theory, belong to two different people. In my situation though, I don't foresee that being enough of a likelihood that I would need to enforce this on login.

我正在使用Postgres数据库,因此我希望将 Auth :: attempt()中的电子邮件数据库查找替换为 ilike 而不是喜欢(如果可能).但是,我终生无法找到源代码所在的位置,因此不知道我将如何覆盖它.

I'm using a Postgres database so I'm looking to just replace the email database lookup in Auth::attempt() with ilike instead of like if possible. However, I can't for the life of me find where this is in the source and therefore have no idea how I would go about overwriting this.

Tl; dr我如何重写Laravel的默认身份验证以不区分大小写的方式进行电子邮件查找?

Tl;dr how do I override Laravel's default authentication to do email lookup in a case insensitive manner?

我最终通过首先接受@btl建议并在email属性上实现了mutator方法来解决了该问题.我不想遍历并更改现有用户的所有电子邮件,并且感觉好像我遇到了想要恢复区分大小写的问题时,我想要一个解决方案,该解决方案可以轻松撤消.

I ended up solving the issue by first taking @btl advice and implementing a mutator method on the email attribute. I didn't want to have to go through and change all the emails for existing users, and felt like in case I ever did run into an issue in which I'd like to have case-sensitivity back, I wanted a solution that could be undone easily.

app/User.php

public function getEmailAttribute($value) {
    return strtolower($value);
}

app/Http/Controllers/Auth/LoginController.php

protected function credentials()
{
    $username = $this->username();
    $credentials = request()->only($username, 'password');
    if (isset($credentials[$username])) {
        $credentials[$username] = strtolower($credentials[$username]);
    }
    return $credentials;
}

不幸的是,我无法弄清楚如何将相同的逻辑应用于 ForgotPasswordController.php ,但是现在我将继续生活.现在无论电子邮件大小写,登录都可以正常工作.

Unfortunately I couldn't figure out how to apply this same logic to the ForgotPasswordController.php but for now I'll live. Logging in seems to work regardless of email case now.

推荐答案

在用户模型上添加mutator方法,使电子邮件全部变为小写:

Add an mutator method on your user model to make the email all lowercase:

public function setEmailAttribute($value)
{
    $this->attributes['email'] = strtolower($value);
}

每当用户注册时,电子邮件将始终转换为小写字母.

Whenever a user registers, the email will always be converted to lower case.

或者,在用户模型上使用访问器:

Or, use an accessor on the user model:

public function getEmailAttribute($value)
{
    return strtolower($value);
}

这样,原始值将保留在数据库中,但是每当调用$ user-> email时,它将变为小写.进行严格比较时,只需将登录时的用户输入转换为小写即可.

That way the original value is preserved in the database, but whenever you call $user->email it will be lowercase. You'd just need to convert your user's input when logging to to lowercase when making the strict comparison.

这篇关于如何使用Laravel 5.5身份验证使电子邮件登录大小写不敏感的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆