Php Laravel-4:如何向AUTH模块添加自定义字段
如何将自定义字段添加到Php Laravel-4:如何向AUTH模块添加自定义字段,php,laravel,Php,Laravel,如何将自定义字段添加到AUTH模块?您必须向AUTH::trunt方法发送一个带有用户名和密码的数组 不过,你可以用你需要的任何东西填充它们,在你的情况下,应该是这样的: $cred = array( 'username' => $post['username'], 'password1' => md5($post['password1']), 'password2' => md5($post['password2']) // custom third
AUTH
模块?您必须向AUTH::trunt
方法发送一个带有用户名和密码的数组
不过,你可以用你需要的任何东西填充它们,在你的情况下,应该是这样的:
$cred = array(
'username' => $post['username'],
'password1' => md5($post['password1']),
'password2' => md5($post['password2']) // custom third field
);
// Check all for authentication
Auth::attempt($cred);
$post = Input::all(); // I assume this is how you are filling the $post variable?
Auth::attempt(array('username' => $post['username'], 'password' => $post['password1']);
请注意,您不需要散列密码,尝试方法将处理该问题。您也不需要同时发送第二个密码,它将完全忽略除“用户名”和“密码”之外的所有内容。您不需要,您可以尝试以下方式:
$cred = array(
'username' => $post['username'],
'password1' => md5($post['password1']),
'password2' => md5($post['password2']) // custom third field
);
// Check all for authentication
Auth::attempt($cred);
$post = Input::all(); // I assume this is how you are filling the $post variable?
Auth::attempt(array('username' => $post['username'], 'password' => $post['password1']);
在视图中
可以使用此选项显示登录失败时的错误消息:
$cred = array(
'username' => Input::get('username'),
'password' => Input::get('password1')
);
// At first normally check the credentials using validate method
// but doesn't login, if check succeeded then check the second
// password manually, using your encrypted password2 field
if(Auth::validate($cred)) {
// Now check the password2, assumed you're using md5
// hashing for this field (according to your question)
$password2 = md5(Input::get('password2'));
$user = User::where('username', $cred['username'])->first();
if( $user->password2 == $password2) {
// Now manually login the user
Auth::login($user);
return Redirect::to('url'); // successful login url
}
else{
// Password2 missmatched
$validator = Validator::make(array(), array())->getMessagebag();
$validator->add('errorLogin', 'Invalid Credentials!');
return Redirect::back()->withInput()->withError($validator);
}
}
else {
// Didn't pass even the first validation (username, password1)
$validator = Validator::make(array(), array())->getMessagebag();
$validator->add('errorLogin', 'Invalid Credentials!');
return Redirect::back()->withInput()->withError($validator);
}
对于第一种验证方法,不要使用任何加密,让Laravel按它的方式进行,对于第二种密码,您可以使用自己的加密。如果您想让用户与另一个凭据/where子句匹配,只需将其传递到凭据数组中即可。例如:
{{ $errors->first('errorLogin') }} // Invalid Credentials!
如果我假设这些是
中的post变量。。然后你应该想$\u POST['username']
@ShankarDamodaran我的意思是如何让模块使用自定义的'username'和'password1'和'password2'对用户进行身份验证,而不是标准的'login'和'password'@ShankarDamodaran With Laravel,你将使用Input::get('name'))
不是全局变量。您总是希望在接触数据库之前处理验证。最重要的是,如果要比较密码,您需要执行Hash::check('password','hashofpassword')
。不过,这里不需要这样做,因为您也可以这样做,然后让用户登录。我还想补充一点,试图让用户在登录时提供两次密码是一个可怕的想法。@ollieread!先看问题,然后看我的答案。OP使用两个不同的字段作为密码,他有一个使用md5哈希保存到数据库中的密码,另一个使用普通方式,所以他必须使用这个东西。至少你应该想到,OP接受了它,因为它解决了问题。这不是关于如何实现完全身份验证,而是关于如何使用两个不同的密码对用户进行身份验证,我故意省略了验证,只是专注于解决方案。我想你会发现密码是一个示例字段。通过password1和password2都在使用md5()
这一事实可以看出这一点。此外,您不应该使用两个不同的密码对用户进行身份验证,而且您最肯定的是不应该使用普通的md5。你在鼓励坏代码。如前一个问题所述,这是如何在传递另一个凭证时使用Auth
库。你的答案错得离谱。它不是同一个密码,两个不同的密码字段有两个不同的哈希值,一个是uingmd5
@sheikheera。你从哪里获得此信息的?我看不到有人提到这件事。投票否决某人的信息似乎不公平。最初的问题是关于向身份验证调用添加额外字段。请阅读注释,如何使模块使用自定义的“用户名”、“密码1”和“密码2”而不是标准的“登录”和“密码”对用户进行身份验证。
,我不鼓励这样做,但这是OP
的需要,我用另一种方式解决了它,答案被接受了,但是的,我会在我即将出版的关于Laravel的书中写下最佳实践(Laravel)。除此之外,你已经用几行代码回答了,在我看来,这是正确的答案。需要稍加修改,但对我来说很有魅力。我认为OP需要这个答案。至少,当有人用谷歌搜索这个问题时,这里是问题的正确答案。如果这不是OP想要的答案,那么这个问题就不好了。事实上,它只需要“密码”,而不需要忽略其他一切。