Check whether password is correct or not in Laravel

后端 未结 6 1584
傲寒
傲寒 2020-12-31 00:23

In laravel I want to check if user enter current password than check with that password which is store in database in that user data. If correct than continue otherwise give

6条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-31 00:39

    As Hiren has mentioned you can use the default registered hasher as that is passed to the specific UserProvider used. The default is Illuminate\Hashing\BcryptHasher.

    You can use it a couple of ways:

    1. Out of the container
    $user = User::find($id);
    $hasher = app('hash');
    if ($hasher->check('passwordToCheck', $user->password)) {
        // Success
    }
    
    1. Using the Facade
    $user = User::find($id);
    if (Hash::check('passwordToCheck', $user->password)) {
        // Success
    }
    
    1. Out of interest using the generic php function password_verify also works. However that works because the default hashing algorithm it uses is bcrypt.
    if (password_verify('passwordToCheck', $user->password)) {
        // Success
    }
    

提交回复
热议问题