Laravel – Change Password with Current Password Validation Example

Laravel – Change Password with Current Password Validation Example

In this post we will give you information about Laravel – Change Password with Current Password Validation Example. Hear we will give you detail about Laravel – Change Password with Current Password Validation ExampleAnd how to use it also give you demo for it if it is necessary.

Today, we will learn to update password with checking old password in laravel 5 application. we will create function to change password with old password validation rule in laravel. we will do old password verification using custom validation rule in laravel 5.

In this tutorial, we will create example from starch. first we will create auth then we create change password page. after that we will create our custom validation rules for checking with current password on database. then we use that custom validation rules on our controller file.

So, you need to just follow few step to get complete guide for validate old password using custom validation in laravel. you can see bellow attach screen shot to layout of our example:

Preview

Step 1 : Install Laravel 5.8

first of all we need to get fresh Laravel 5.8 version application using bellow command, So open your terminal OR command prompt and run bellow command:

composer create-project --prefer-dist laravel/laravel blog

Step 2: Create Custom Validation Rule

After you can setup for migration and basic auth. i didn’t write here for run migration and basic auth because you know how to do that. now we need to create new validation rules using following command:

php artisan make:rule MatchOldPassword

After this we can see they created new “Rules” folder with one file on app directory. you can update on that file like this way:

app/Rules/MatchOldPassword.php

<?php

namespace AppRules;

use IlluminateContractsValidationRule;

use IlluminateSupportFacadesHash;

class MatchOldPassword implements Rule

{

/**

* Determine if the validation rule passes.

*

* @param string $attribute

* @param mixed $value

* @return bool

*/

public function passes($attribute, $value)

{

return Hash::check($value, auth()->user()->password);

}

/**

* Get the validation error message.

*

* @return string

*/

public function message()

{

return 'The :attribute is match with old password.';

}

}

Also see:How to increment or decrement a column value in Laravel?

Step 3: Create Routes

Here, we need to add two route for view and another for post route. so open your “routes/web.php” file and add following route.

routes/web.php

Route::get('change-password', 'ChangePasswordController@index');

Route::post('change-password', 'ChangePasswordController@store')->name('change.password');

Step 4: Create New Controller

In this step, now we should create new controller as ChangePasswordController. In this file we will create two method index() and store(). we will also use validation here. so let’s do.

app/Http/Controllers/ChangePasswordController.php

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;

use AppRulesMatchOldPassword;

use IlluminateSupportFacadesHash;

use AppUser;

class ChangePasswordController extends Controller

{

/**

* Create a new controller instance.

*

* @return void

*/

public function __construct()

{

$this->middleware('auth');

}

/**

* Show the application dashboard.

*

* @return IlluminateContractsSupportRenderable

*/

public function index()

{

return view('changePassword');

}

/**

* Show the application dashboard.

*

* @return IlluminateContractsSupportRenderable

*/

public function store(Request $request)

{

$request->validate([

'current_password' => ['required', new MatchOldPassword],

'new_password' => ['required'],

'new_confirm_password' => ['same:new_password'],

]);

User::find(auth()->user()->id)->update(['password'=> Hash::make($request->new_password)]);

dd('Password change successfully.');

}

}

Step 5: Create Blade File

In this step, we need to create changePassword blade file and we will write code for form create and display error messages. so let’s create file and put bellow code:

resources/views/changePassword.blade.php

@extends('layouts.app')

@section('content')

<div >

<div >

<div >

<div >

<div >Laravel - Change Password with Current Password Validation Example - ItSolutionStuff.com</div>

<div >

<form method="POST" action="{{ route('change.password') }}">

@csrf

@foreach ($errors->all() as $error)

<p >{{ $error }}</p>

@endforeach

<div >

<label for="password" >Current Password</label>

<div >

<input id="password" type="password" name="current_password" autocomplete="current-password">

</div>

</div>

<div >

<label for="password" >New Password</label>

<div >

<input id="new_password" type="password" name="new_password" autocomplete="current-password">

</div>

</div>

<div >

<label for="password" >New Confirm Password</label>

<div >

<input id="new_confirm_password" type="password" name="new_confirm_password" autocomplete="current-password">

</div>

</div>

<div >

<div >

<button type="submit" >

Update Password

</button>

</div>

</div>

</form>

</div>

</div>

</div>

</div>

</div>

@endsection

Now we are ready to run our example so run bellow command so quick run:

php artisan serve

Now you can open bellow url on your browser:

Also see:Laravel Update User Status Using Toggle Button Example

http://localhost:8000/

You can download code from git: Download Code from Github

I hope it can help you…

Hope this code and post will helped you for implement Laravel – Change Password with Current Password Validation Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

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

22  +    =  25

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US