Check whether password is correct or not in Laravel

后端 未结 6 1585
傲寒
傲寒 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
    }
    
    0 讨论(0)
  • 2020-12-31 00:39
    1. Hit the server route with your AJAX call.
    2. Check the result of auth()->attempt($request->only('email', 'password'))
    3. Return JSON back with a message and HTTP status code (200 if it is successful, 400 if it failed).
    4. Show the message in the success() method to a div. If it is an error, show the error message inside the error() method to a div.
    0 讨论(0)
  • 2020-12-31 00:45

    Just do it

    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'=>'password is correct']);
    
        } else {
            return 'false';
        }
    
    }
    
    0 讨论(0)
  • 2020-12-31 00:47

    When the user attempts to access the page, redirect them to an auth page.

    Do the ajax call then do the following in your php:

    public function check(Request $request)
    {
        if(Hash::check($request->password, $user->password)) {
            // They match
        } else {
            // They don't match
        }
    }
    

    I havn't tested this so it might not work.

    0 讨论(0)
  • 2020-12-31 00:47

    if (Hash::check($request->password, Auth::user()->password)) { //Sucess }

    0 讨论(0)
  • you can use hash:check method.

    create password using hash:

    $password = Hash::make('secret');
    

    check password:

    if (Hash::check('secret', $hashedPassword))
    {
        // The passwords match...
    }
    
    0 讨论(0)
提交回复
热议问题