Laravel Change Password OLD Password Validation Example

May 2, 2020 | Category : Laravel

Here, i will show you old password validation laravel. you'll learn password verification laravel. This article will give you simple example of verify password in laravel. you'll learn update password with validate current password laravel. So, let's follow few step to create example of change password validation in laravel.

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 7

first of all we need to get fresh Laravel 7 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 App\Rules;

use Illuminate\Contracts\Validation\Rule;

use Illuminate\Support\Facades\Hash;

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.';

}

}

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 App\Http\Controllers;

use Illuminate\Http\Request;

use App\Rules\MatchOldPassword;

use Illuminate\Support\Facades\Hash;

use App\User;

class ChangePasswordController extends Controller

{

/**

* Create a new controller instance.

*

* @return void

*/

public function __construct()

{

$this->middleware('auth');

}

/**

* Show the application dashboard.

*

* @return \Illuminate\Contracts\Support\Renderable

*/

public function index()

{

return view('changePassword');

}

/**

* Show the application dashboard.

*

* @return \Illuminate\Contracts\Support\Renderable

*/

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 class="container">

<div class="row justify-content-center">

<div class="col-md-8">

<div class="card">

<div class="card-header">Laravel - Change Password with Current Password Validation Example - HDTuto.com</div>

<div class="card-body">

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

@csrf

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

<p class="text-danger">{{ $error }}</p>

@endforeach

<div class="form-group row">

<label for="password" class="col-md-4 col-form-label text-md-right">Current Password</label>

<div class="col-md-6">

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

</div>

</div>

<div class="form-group row">

<label for="password" class="col-md-4 col-form-label text-md-right">New Password</label>

<div class="col-md-6">

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

</div>

</div>

<div class="form-group row">

<label for="password" class="col-md-4 col-form-label text-md-right">New Confirm Password</label>

<div class="col-md-6">

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

</div>

</div>

<div class="form-group row mb-0">

<div class="col-md-8 offset-md-4">

<button type="submit" class="btn btn-primary">

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:

http://localhost:8000/

You can download code from git: Download Code from Github

I hope it can help you...