if (Hash::check($request['password', $this->password))
Verify Password is the same , and Update it .
Hi , I am trying to update the password of the user . But before update , the user must type the same password , like this -
- verify if the current password is the same .
- update the password . ( new one )
I am making like this ->
if( $this->password != bcrypt($request['password'])){
return true;
}
But It is not working like this with bcrypt() .
Does some one knows how could I do it ?
Thanks
Please go through this documentation regarding this. https://laravel.com/docs/5.7/authentication#the-user-provider-contract https://laravel.com/docs/5.7/hashing
if (Hash::check($request['password'], $user->getAuthPassword() )) {
// The passwords match...
}
Thanks.
On Your Request
//For Exmaple ValidateOldPassword
/**
* Configure the validator instance.
*
* @param \Illuminate\Validation\Validator $validator
* @return void
*/
public function withValidator($validator)
{
$validator->after(function ($validator) {
if ($this->has('old_password') && !Hash::check($this->old_password, \Auth::user()->password)) {
$validator->errors()->add('old_password', 'كلمة المرور القديمة ليست صحيحة');
}
});
}
Controller::ChangePasswordController.php
<?php
namespace App\Http\Controllers;
use Auth;
use Hash;
use Illuminate\Http\Request;
class ChangePasswordController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function showChangePasswordForm(){
return view('auth.changepassword');
}
public function changePassword(Request $request){
if (!(Hash::check($request->get('current-password'), Auth::user()->password))) {
// The passwords matches
return redirect()->back()->with("error","Your current password does not matches with the password you provided. Please try again.");
}
if(strcmp($request->get('current-password'), $request->get('new-password')) == 0){
//Current password and new password are same
return redirect()->back()->with("error","New Password cannot be same as your current password. Please choose a different password.");
}
$validatedData = $request->validate([
'current-password' => 'required',
'new-password' => 'required|string|min:6|confirmed',
]);
//Change Password
$user = Auth::user();
$user->password = bcrypt($request->get('new-password'));
$user->save();
return redirect()->back()->with("success","Password changed successfully !");
}
}
---------
routes::web.php
Route::get('/changePassword','ChangePasswordController@showChangePasswordForm');
Route::post('/changePassword','ChangePasswordController@changePassword')->name('changePassword');
---------
view::changepassword.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Change password</div>
<div class="card-body">
@if (session('error'))
<div class="alert alert-danger">
{{ session('error') }}
</div>
@endif
@if (session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
@endif
<form class="form-horizontal" method="POST" action="{{ route('changePassword') }}">
@csrf
<div class="form-group{{ $errors->has('current-password') ? ' has-error' : '' }}">
<label for="new-password" class="col-md-4 control-label">Current Password</label>
<div class="col-md-6">
<input id="current-password" type="password" class="form-control" name="current-password" required>
@if ($errors->has('current-password'))
<span class="help-block">
<strong>{{ $errors->first('current-password') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('new-password') ? ' has-error' : '' }}">
<label for="new-password" class="col-md-4 control-label">New Password</label>
<div class="col-md-6">
<input id="new-password" type="password" class="form-control" name="new-password" required>
@if ($errors->has('new-password'))
<span class="help-block">
<strong>{{ $errors->first('new-password') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group">
<label for="new-password-confirm" class="col-md-4 control-label">Confirm New Password</label>
<div class="col-md-6">
<input id="new-password-confirm" type="password" class="form-control" name="new-password_confirmation" required>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Change Password
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
Hello @kiruba , @zymawy , @BezhanSalleh , I can see that is using Hash:: Instead the bcript() . To incript my password , do I use Hash:: instead bcript as well ? is that more , or less secure than bcript() ?
@MURILO - Both are same. bcrypt() is just a helper function to hash::make() https://laravel.com/docs/5.7/helpers#method-bcrypt
Thanks.
Thanks @kiruba
The docs have a section on re-setting passwords, just follow and work an example: https://laravel.com/docs/5.7/passwords
Please or to participate in this conversation.