拉拉维尔密码哈希验证



我在Laravel 6.X中编码了自定义用户系统。在我的登录控制器中,我有以下验证码:

$email = $request->email;
$validator = Validator::make($request->all(), [
'email' => 'bail|email|required|max:32|exists:users,email',
'password' => ['required','min:1','max:32',
function ($attribute, $value, $fail) {
$users = DB::table('users')->where('email', $email)->get(); // Get user info (Exception is in here)
if (hash('sha256', $value) !== $users[0]->Password) { // Check if hashed password equals
$fail($attribute.' is wrong.'); // If not equal then return error message
}
},
],
]);

我使用 SHA256 哈希存储我的密码。如果密码不等于数据库密码,则向用户返回错误消息。但是我收到一个异常"未定义的变量:电子邮件"。

看看这个答案:https://stackoverflow.com/a/11086796/7897328。我认为您需要使用use关键字。

您的代码:

function ($attribute, $value, $fail) {

带有use关键字的代码:

function ($attribute, $value, $fail) use ($email) {

最新更新