Hello Devs,
In this tutorial, we are going to learn how to change password functionality with validations in laravel app.
Follow this step by step guide given below:
Step 1 :
Install Laravel
composer create-project --prefer-dist laravel/laravel blog
Step 2 :
Add Route
Route::get('/reset-password','ResetPasswordController@create');
Route::post('/reset-password','ResetPasswordController@store')->name('reset.password.store');
Step 3 :
Create New Controller app/Http/Controllers/ResetPasswordController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class ResetPasswordController extends Controller
{
public function create()
{
return view('resetPassword');
}
public function store(Request $request)
{
$request->validate([
'current_password' => 'required',
'new_password' => 'required|string|min:6|same:confirm_password',
'confirm_password' => 'required',
]);
$user = \Auth::user();
if (!\Hash::check($request->current_password, $user->password)) {
return back()->with('error', 'Current password does not match!');
}
$user->password = \Hash::make($request->new_password);
$user->save();
return back()->with('success', 'Password successfully changed!');
}
}
Step 4 :
Create View File resources/views/resetPassword.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel Reset Password Functionality with Validations - Rathorji.in</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha256-916EbMg70RQy9LHiGkXzG8hSg9EdNy97GazNG/aiY1w=" crossorigin="anonymous" />
<link rel="stylesheet" type="text/css" href="https://jhollingworth.github.io/bootstrap-wysihtml5//lib/css/bootstrap.min.css"></link>
<link rel="stylesheet" type="text/css" href="https://jhollingworth.github.io/bootstrap-wysihtml5//lib/css/prettify.css"></link>
<link rel="stylesheet" type="text/css" href="https://jhollingworth.github.io/bootstrap-wysihtml5//src/bootstrap-wysihtml5.css"></link>
</head>
<body class="wysihtml5-supported">
<div class="container">
<div class="row" style="margin-top: 50px;">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">
<h3 style="color: black;"> Laravel Reset Password Functionality with Validations - Rathorji.in</h3>
</div>
<div class="panel-body">
@if (count($errors) > 0)
<div class="row">
<div class="col-md-12">
<div class="alert alert-danger alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i class="icon fa fa-ban"></i> Error!</h4>
@foreach($errors->all() as $error)
{{ $error }} <br>
@endforeach
</div>
</div>
</div>
@endif
<form class="image-upload" method="post" action="{{ route('reset.password.store') }}" enctype="multipart/form-data">
@csrf
<div class="form-group">
<label>Current Password</label>
<input type="password" name="current_password" class="form-control"/>
</div>
<div class="form-group">
<label>New Password</label>
<input type="password" name="new_password" class="form-control"/>
</div>
<div class="form-group">
<label>Confirm Password</label>
<input type="password" name="confirm_password" class="form-control"/>
</div>
<div class="form-group text-center">
<button type="submit" class="btn btn-success btn-sm">Save</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
Run this command:
php artisan serve
Open this URL:
http://localhost:8000/reset-password
I hope this example helps you.