Change Password – Laravel

Following is the code to be used inside controller.

2 functions is declared, one is for requesting and another is for processing

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

use App\User;

class ProfileController extends Controller
{
    public function password()
    {
    	

        $title = "..:::: Benera Pty. Ltd. - Profile Detail";

        $update = false;

        return view('admin.profile.password',compact('title','update'));
    }


     public function passwordStore(Request $request)
    {
    	$data = $request->validate([
    		'old_password' => 'required|min:6|max:15',
    		'new_password' => 'required|min:6|max:15',
        	'confirm_password' => 'required|min:6|max:15|same:new_password'
    	],[
    		
    		'confirm_password.same' => 'New and Confirmed Password Does Not Matched',
    	]);


    	$request_old = $data['old_password'];
    	$old_hashed = Auth::user()->password;

    	if (Hash::check($request_old, $old_hashed)) {
    

    		$new_password = $data['new_password'];

    		$new_hashed  = Hash::make($new_password);

    		$user_id = Auth::user()->id;

    		User::where('id',$user_id)->update([
    			'password' => $new_hashed
    		]);

    		return redirect()->back()->with('success','Password Changed Successfully');
		}
		else
		{
			return redirect()->back()->with('danger','Invalid Old Password');
		}
    }

}

Leave a Reply

Your email address will not be published. Required fields are marked *