Php 如何为Laravel Auth创建自定义密码字段

Php 如何为Laravel Auth创建自定义密码字段,php,laravel,laravel-5.8,laravel-authentication,Php,Laravel,Laravel 5.8,Laravel Authentication,我需要登录在Igrejas表中注册的用户,这些用户具有responsavel\u cpf和responsavel\u senha字段,但函数validateRedentials中的Laravel需要名称“password” 公共函数validateCredentials(UserContract$user,array$credentials) { $plain=$credentials['password']; } 我尝试使用尝试登录,但未成功: $userdata=array( 'respo

我需要登录在Igrejas表中注册的用户,这些用户具有responsavel\u cpfresponsavel\u senha字段,但函数validateRedentials中的Laravel需要名称
“password”

公共函数validateCredentials(UserContract$user,array$credentials)
{
$plain=$credentials['password'];
}
我尝试使用尝试登录,但未成功:

$userdata=array(
'responsavel_cpf'=>Input::get('email'),
'responsavel_senha'=>Input::get('password')
);
if(Auth::guard('igrejas')->尝试($userdata)){
return Redirect::to('dashboard_paroquia');
}否则{
返回Redirect::to('login');
}

如何将默认字段emailpassword替换为responsavel\u cpfresponsavel\u senha

您可以这样覆盖用户模型中的密码列:

//User.php
公共函数getAuthPassword()
{
返回$this->custom_pw_字段;
}
但是,如果您确实希望将不显式包含
密码的数组传递给
Auth::guard('xxx')->trunt($credentials)
(没有理由!),您可能需要手动覆盖并添加
illights\Auth\EloquentUserProvider
,这似乎需要大量工作

因此,我建议只使用以下方法:

Auth::guard('xxx')->尝试([
'email'=>$request->post('email'),
'password'=>$request->post('password')
]);
然后,
密码
键应根据您定义的
自定义字段
进行验证

解释 通过查看
illumb\Auth\EloquentUserProvider的源代码并检查函数
public function retrieveByCredentials(array$credentials)
,您可以看到它的功能:

  • 在auth表中查找与
    $credentials
    数组中除
    password
    以外的所有条件匹配的第一条记录(在上面的示例中,只需
    email
    )。例如,您可以添加另一个键,如
    is_webmaster
    $authGuard->trunt(['email'=>$request->post('email'),'is_webmaster'=>1])
    ,然后检索具有这些属性的第一条用户记录
  • 检索此记录后,将根据输入检查密码列中的哈希值

您可以覆盖用户模型中的密码列,如下所示:

//User.php
公共函数getAuthPassword()
{
返回$this->custom_pw_字段;
}
但是,如果您确实希望将不显式包含
密码的数组传递给
Auth::guard('xxx')->trunt($credentials)
(没有理由!),您可能需要手动覆盖并添加
illights\Auth\EloquentUserProvider
,这似乎需要大量工作

因此,我建议只使用以下方法:

Auth::guard('xxx')->尝试([
'email'=>$request->post('email'),
'password'=>$request->post('password')
]);
然后,
密码
键应根据您定义的
自定义字段
进行验证

解释 通过查看
illumb\Auth\EloquentUserProvider的源代码并检查函数
public function retrieveByCredentials(array$credentials)
,您可以看到它的功能:

  • 在auth表中查找与
    $credentials
    数组中除
    password
    以外的所有条件匹配的第一条记录(在上面的示例中,只需
    email
    )。例如,您可以添加另一个键,如
    is_webmaster
    $authGuard->trunt(['email'=>$request->post('email'),'is_webmaster'=>1])
    ,然后检索具有这些属性的第一条用户记录
  • 检索此记录后,将根据输入检查密码列中的哈希值

您使用的是哪个laravel版本?您是否更改了配置>auth.php文件“web”=>[“driver”=>“session”,“provider”=>“customusers”,],@AmitKumar,我使用的是5.8.xversion@JadavPalak我尝试了这个,但他使用的是同样的函数,需要$plain=$credentials['password']您正在使用哪个laravel版本?您是否更改了配置>auth.php文件“web”=>[“driver”=>“session”,“provider”=>“customusers”,],@AmitKumar,我正在使用5.8.xversion@JadavPalak我尝试了这个,但他使用的是同样的函数,需要$plain=$credentials['password']他的解决方案的问题是,responsavel_cpf不会替换电子邮件。如果您仔细阅读,您可以看到您可以自由地用表中的任何其他列替换
电子邮件
,因为它基本上被添加到查询中,从而为您提供待验证的记录。他的解决方案的问题是,responsavel_cpf不会替换电子邮件。如果您充分阅读它,您可以看到您可以使用表中的任何其他列自由替换
email
,因为它基本上会被添加到查询中,而查询会给出待验证的记录。