在laravel中,我想检查用户是否输入当前密码,而不是检查该用户数据中存储在数据库中的密码。如果正确而不是继续,否则给出消息密码不正确。
我是laravel的新手,所以没有得到确切的想法。
提前致谢。
$('#pwd').blur(function(){
var oldpwd=$('#pwd').val();
var uid = $('#id').val();
console.log(uid);
if(oldpwd != "")
{
$.ajax({
url : "{{ url/profile/checkOldPwd}}",
data : { oldpwd:oldpwd , uid:uid },
type : "POST",
success : function(data) {
if(data == 0){
$("#msg-old").show();
$("#msg-old").html("Password is Incorrect!");
$("#pwd").val("");
$("#pwd").focus();
}
else
{
$("#msg-old").hide();
$("#msg-old").html("");
}
}
});
}
});
正如Hiren所提到的,您可以使用默认的已注册哈希,因为它已传递给使用的特定UserProvider。默认值为Illuminate\Hashing\BcryptHasher
。
您可以通过以下两种方式使用它:
$user = User::find($id);
$hasher = app('hash');
if ($hasher->check('passwordToCheck', $user->password)) {
// Success
}
$user = User::find($id);
if (Hash::check('passwordToCheck', $user->password)) {
// Success
}
password_verify
也有效。但是这可行,因为它使用的默认哈希算法是bcrypt。if (password_verify('passwordToCheck', $user->password)) {
// Success
}
你可以使用hash:check方法。
使用哈希创建密码:
$password = Hash::make('secret');
检查密码:
if (Hash::check('secret', $hashedPassword))
{
// The passwords match...
}
当用户尝试访问该页面时,将其重定向到auth页面。
然后执行ajax调用在php中执行以下操作:
public function check(Request $request)
{
if(Hash::check($request->password, $user->password)) {
// They match
} else {
// They don't match
}
}
我没有对此进行测试,因此可能无法正常工作。
auth()->attempt($request->only('email', 'password'))
的结果success()
方法中的消息显示为div。如果是错误,请将error()
方法中的错误消息显示为div。去做就对了
public function authenticateEmployee(array $data){
$email = $data['email'];
$password = $data['password'];
$user = User::where('email', '=', $email)->first(); //get db User data
if(Hash::check($password, $user->password)) {
return response()->json(['status'=>'false','message'=>'passwor is correct']);
} else {
return 'false';
}
}