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


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

我正在使用Laravel创建一个网站,我希望使用Auth中间件。为了让用户添加多个电子邮件地址和电话号码,以便他们有备份选项进行恢复等,我将它们存储在与主users表不同的表中。

仔细查看身份验证文档并浏览用于身份验证的一些类,我不确定如何指定应从各自的表中检索电子邮件和电话号码,而不是从users表中检索(email_addressesphone_numbers)。

如果有人能告诉我如何设置它,或者至少为我指出正确的方向,我将不胜感激。不需要拼写出来,但如果有人知道我需要编辑哪些文件,那将有很大帮助。

通过电子邮件从电子邮件表中获取用户记录:

$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!'
}
相关文章: