简体   繁体   English

Laravel:如何通过电子邮件对用户进行身份验证,其中电子邮件地址存储在单独的表中?

[英]Laravel: How can I authenticate a user by e-mail, where the e-mail address is stored in a separate table?

I'm creating a website using Laravel and I wish to use the Auth middleware.我正在使用 Laravel 创建一个网站,我希望使用 Auth 中间件。 To let users add multiple e-mail addresses and phone numbers, so they have backup options for recovery etc., I'm storing them in separate tables from the main users table.为了让用户添加多个电子邮件地址和电话号码,以便他们有用于恢复等的备份选项,我将它们存储在与主users表不同的表中。

Looking closer at the Authentication documentation and skimming through some of the classes used for the authentication, I'm unsure how I might specify that the e-mail and phone number should be retrieved from their respective tables rather than the users table ( email_addresses and phone_numbers ).仔细查看身份验证文档并浏览一些用于身份验证的类,我不确定如何指定应从各自的表而不是users表( email_addressesphone_numbers检索电子邮件和电话号码)。

If anyone could tell me how to set this up, or at least point me in the right direction, I would be super grateful.如果有人能告诉我如何设置它,或者至少为我指明正确的方向,我将不胜感激。 Don't need to have it spelled out, but if someone know which files I need to edit that would help a lot.不需要详细说明,但如果有人知道我需要编辑哪些文件,那将大有帮助。

Get your user record by email from emails table:通过电子邮件从电子邮件表中获取您的用户记录:

$record = Emails::where('email', $email)->first();

if($record){
    //if this email is valid you will get the user record
    if(auth()->validate(['id' => $record->user_id, 'password' => $input['password']])) auth()->loginUsingId($record->user_id)
    else echo 'Invalid password';
}
else {
    echo 'User not present!'
}

Insane circumstances come up when customers requests something that you haven't seen.当客户要求您没有见过的东西时,就会出现疯狂的情况。 In any case, this gives us the best approach to learn and I like these sort of customers.无论如何,这为我们提供了最好的学习方法,我喜欢这类客户。

protected function credentials(Request $request)
{
return $request->only('customer_id', 'password', 'dob');
}

And inserting this in LoginController.php .并将其插入 LoginController.php 。

We have to abrogate one more strategy to make it work, and that is validateLogin() .我们必须废除另一种策略才能使其发挥作用,那就是 validateLogin() 。 It is utilized for approving the solicitation from login.blade.php它用于批准来自 login.blade.php 的请求

/**
* Validate the user login request.
*
* @param \Illuminate\Http\Request $request
* @return void
*/
protected function validateLogin(Request $request)
{
$this->validate($request, [ 'customer_id' => 'required|string',
'password' => 'required|string',
'dob' => 'required|date'
]);
}

you can check this from here你可以从这里检查这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在外部php文件中获取登录用户的当前Wordpress电子邮件地址? - How can I get the current Wordpress e-mail address of a logged-in user in an external php file? 我可以检查电子邮件地址是否有效吗? - Can I check if e-mail address is valid? Laravel添加?发邮件 - Laravel Adding ? to E-mail Laravel电子邮件验证 - Laravel E-mail Validation 如何将收件人的电子邮件地址传递给Mail :: send()内部回调函数? - How can I dinamically pass the recipient e-mail address into the Mail::send() inner callback function? 如何使用PHP将主机example.com与诸如user@example.com之类的电子邮件地址分开? - How I separate the host example.com from an e-mail address like user@example.com using PHP? 我们在 Laravel 5 身份验证中找不到具有该电子邮件地址的用户 - We can't find a user with that e-mail address in Laravel 5 authentication 阻止在Laravel 5.8中发送验证电子邮件地址电子邮件 - Block the verify e-mail address e-mail from being sent in Laravel 5.8 如何理解电子邮件地址是否是教育电子邮件地址? - How to understand if an e-mail address is an education e-mail address or not? 如何在LimeSurvey中禁用注册电子邮件确认? - How can I disable in LimeSurvey the Registration e-mail confirmation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM