Laravel Reset Password Functionality with Validations

10-Apr-2023

.

Admin

Laravel Reset Password Functionality with Validations

Hi Guys,

In this blog, I will learn you change password functionality with validations in laravel app. We works on Change Password in laravel functionality with validation errors messages. We will reset password with validation using database password using Hash::check method.

So while you are going to change your password then it first validate with database password using Hash::check if match then you update your password with new password.

Here I will give you full example for laravel reset password functionality with validations you can follow bellow step by step.

Step 1 : Install Laravel Application


In this step, You have need to get laravel frash application using bellow command. So lets open terminal and run the bellow command:

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

Step 2 : Add Route

In this step, You need to add two route for view and another for post route. So open your "routes/web.php" file and add following route.

Route::get('/reset-password','ResetPasswordController@create');

Route::post('/reset-password','ResetPasswordController@store')->name('reset.password.store');

Step 3 : Create New Controller

In this step, You can create new controller as ResetPasswordController. In this file we will create two method create() and store(). we will also use validation here. so let's do.

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

In this step, You can create resetPassword blade file and we will write code for form create and display error message.So lets create file and put the code

resources/views/resetPassword.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel Reset Password Functionality with Validations - NiceSnippets.com</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 - NiceSnippets.com</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>

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

php artisan serve

Now you can open bellow URL on your browser:

http://localhost:8000/reset-password

It will help you...

#Laravel 7

#Laravel

#Laravel 6