Password Update In Laravel

Password Update In Laravel

In this article, I will tell you about the process of "Password update in Laravel". I explain you password update process in a very simple step.

Update Password In Laravel

Note:- I have already uploaded the article on the "Laravel Crud Operation".

To update the password we need to create the two routes in the route file and two functions in the controller file.

// password
Route::get('/passwordform/{id}', [CrudController::class, 'passwordview'])->name('passwordview.data');
Route::post('/passwordupdatesubmit/{id}', [CrudController::class, 'updatepassword'])->name('passwordupdatesubmit.data');

Explain the first route code

This Laravel code snippet defines a route in a web application using the Laravel framework. The route is set up to handle HTTP GET requests to the URI path "/passwordform/{id}". The "{id}" in the path signifies a dynamic parameter, indicating that the route expects an 'id' parameter to be included in the URL. When a request is made to this route, the associated logic is delegated to the 'passwordview' method within the "CrudController" class. The "->name('passwordview.data')" part assigns a name to the route, allowing for easy reference and generation of URLs in the application, in this case, the name "passwordview.data" is assigned to the route. Overall, this code establishes a route that triggers the "passwordview" method in the 'CrudController' when accessed, with a dynamic "id" parameter, and it is named "passwordview.data" for convenient URL generation.

Explain the second route code

This Laravel code snippet defines a route for handling HTTP POST requests in a web application. The route is configured with the URI path "/passwordupdatesubmit/{id}", where "{id}" is a dynamic parameter indicating that an 'id' value needs to be provided in the URL. When a POST request is made to this route, the associated logic is delegated to the "updatepassword" method within the 'CrudController' class. Additionally, the route is given the name "passwordupdatesubmit.data". This name can be utilized within the application for generating URLs and redirects associated with this specific route. In practical terms, this route is likely responsible for submitting updates to the password information associated with a specific record in the application's database, as hinted by the "updatepassword" method in the "CrudController". The "id" parameter in the URL is crucial for identifying which record the update pertains to, and the HTTP method being POST indicates that the route is intended for submitting data rather than retrieving it.


The first function of the controller

public function passwordview($id)
{
    $update_view = CrudModel::find($id);
    return view('crud.pages.password', compact('update_view'));
}

Explain the controller function

This PHP code is part of a Laravel web application and represents a method within a controller, likely named `CrudController`. The `passwordview` method takes a parameter `$id`, indicating that it expects an "id" value to be provided when called. Inside the method, a query is executed using Laravel's Eloquent ORM (`CrudModel::find($id)`) to retrieve a record from the 'CrudModel' database table based on the provided 'id'. The result is stored in the variable `$update_view`. Following this, the method returns a view named 'crud.pages.password', passing the data from `$update_view` to the view using the `compact` function. This suggests that the purpose of this method is to fetch a specific record from the database based on the provided 'id' and then render a view, possibly for displaying or updating the password associated with that record. The use of Eloquent's `find` method implies a lookup based on the primary key of the 'CrudModel' table, assuming it follows typical Laravel conventions.

The second function of the controller

public function updatepassword(request $request, $id)
{
    $request->validate([
        'oldpassword' => 'required|min:8',
        'newpassword' => 'required|min:8',
        'newconfirmpassword' => 'required|min:8|same:newpassword',
    ]);
    $updatepassword = CrudModel::find($id);
    $oldpassword = $request->input('oldpassword');
    $newpassword = $request->input('newpassword');
    $confirmpassword = $request->input('newconfirmpassword');
    if($updatepassword)
    {
        $password_check =  Hash::check($oldpassword, $updatepassword->password);
        if($password_check)
        {
            $updatepassword->password = Hash::make($newpassword);
            $updatepassword->update();
            if($updatepassword)
            {
                return redirect()->route('view.data')->with('success', 'Password Has Been Successfully Updated.');
            }
            else
            {
                return redirect()->back()->with('error', 'Password Has Not Been Successfully Updated');
            }
        }
        else
        {
            return redirect()->back()->with('error', 'Old Password Not Match');
        }
    }
}

Explain the second controller function

This PHP code represents a method called "updatepassword" within a Laravel controller, likely named CrudController. The function handles the process of updating a user's password in the application. It first validates the incoming request data using Laravel's validation rules, ensuring that the "oldpassword", "newpassword", and "newconfirmpassword" fields meet the specified criteria. Subsequently, it retrieves the corresponding record from the 'CrudModel' based on the provided "id" parameter.

The code then checks if the old password provided in the request matches the stored hashed password in the database using Laravel's Hash::check method. If the old password is verified, the user's password is updated with the new password, and the record is updated in the database. Depending on the success of the update operation, the function redirects the user to the "view.data" route with a success message, or to the previous page with an error message if the update fails. If the old password does not match, the user is redirected to an error message indicating that the old password is incorrect.

Password Update form code

@extends('crud.layout')
@section('content')
    <div class="container mt-5 p-0 " style="width: 40%">
        <div class="row">
            <div class="col-lg-12">
                <div class="card">
                    <div class="card-header">
                        <h4>Crud Operation In Laravel - Password Update</h4>
                    </div>
                    <div class="card-body">
                        <form action="{{route('passwordupdatesubmit.data', ['id' => $update_view->id])}}" method="post" enctype="multipart/form-data" >
                            @csrf
                            <div class="row">
                                <div class="col-lg-12">
                                    @if (session('error'))
                                        <div class="alert alert-danger">
                                            {{ session('error') }}
                                        </div>
                                    @endif
                                </div>
                            </div>
                            {{-- row-1 --}}
                            <div class="row m-2">
                                <div class="col-lg-12">
                                    <input type="password" name="oldpassword"  class="form-control" autocomplete="off" placeholder="Enter Old Password">
                                    @error('oldpassword')
                                        <div class="alert alert-danger">{{$message}}</div>
                                    @enderror
                                </div>
                                <div class="col-lg-12 mt-3">
                                    <input type="password" name="newpassword"  class="form-control" autocomplete="off" placeholder="Enter New Password">
                                    @error('newpassword')
                                        <div class="alert alert-danger">{{$message}}</div>
                                    @enderror
                                </div>
                                <div class="col-lg-12 mt-3">
                                    <input type="password" name="newconfirmpassword"  class="form-control" autocomplete="off" placeholder="Confirm Password" >
                                    @error('newconfirmpassword')
                                        <div class="alert alert-danger">{{$message}}</div>
                                    @enderror
                                </div>
                            </div>
                            {{-- row-4 --}}
                            <div class="row m-2">
                                <div class="col-lg-12">
                                    <button type="submit" class="submitbutton">SUBMIT</button>
                                    <a href="{{route('view.data')}}" class="cancel_link">Cancel</a>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

Update User Password In Laravel

Explain the code

Extends Layout:

@extends('crud.layout')

The view extends a layout named "crud.layout". This suggests that it inherits the structure and styling defined in that layout file.

Content Section:

@section('content')

This section is used to inject content into the "content" section of the layout. Anything placed within this section will be displayed in the corresponding area of the layout.

HTML Structure:

The view contains HTML and Bootstrap classes to structure and style the password update form.

Form Submission:

<form action="{{route('passwordupdatesubmit.data', ['id' => $update_view->id])}}" method="post" enctype="multipart/form-data" >

The form is set to submit to a route named "passwordupdatesubmit.data", passing the user's ID as a parameter. The method is set to POST, and the form includes CSRF protection with @csrf.

Error Handling:

Error messages are displayed using Laravel's @error directive for each input field.

Form Fields:

The form includes fields for the old password, new password, and password confirmation.

Cancel Link:

<a href="{{route('view.data')}}" class="cancel_link">Cancel</a>

A link is provided to cancel the password update operation and redirect back to the view data page.

Styling:

Various Bootstrap classes are used for styling the form and its components.

Post a Comment

Previous Post Next Post

Recent in Technology