[英]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_addresses
和phone_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'
]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.