Laravel 5.8 change password functionality

Asked
Active3 hr before
Viewed126 times

7 Answers

change
90%

use App\Rules\MatchOldPassword;,Route::get('change-password', 'ChangePasswordController@index');,app/Http/Controllers/ChangePasswordController.php,Step 1 : Install Laravel 5.8

composer create - project--prefer - dist laravel / laravel blog
load more v
88%

Meta Stack Overflow , Stack Overflow Public questions & answers , Stack Overflow help chat ,however your are using request fields current and password to verify current password and set a new one:

try this

public
function updatePassword(Request $request) {
   if (!(Hash::check($request - > get('old_password'), Auth::user() - > password))) {
      // The passwords not matches
      //return redirect()->back()->with("error","Your current password does not matches with the password you provided. Please try again.");
      return response() - > json(['errors' => ['current' => ['Current password does not match']]], 422);
   }
   //uncomment this if you need to validate that the new password is same as old one

   if (strcmp($request - > get('old_password'), $request - > get('new_password')) == 0) {
      //Current password and new password are same
      //return redirect()->back()->with("error","New Password cannot be same as your current password. Please choose a different password.");
      return response() - > json(['errors' => ['current' => ['New Password cannot be same as your current password']]], 422);
   }
   $validatedData = $request - > validate([
      'old_password' => 'required',
      'new_password' => 'required|string|min:6|confirmed',
   ]);
   //Change Password
   $user = Auth::user();
   $user - > password = Hash::make($request - > get('new_password'));
   $user - > save();
   return $user;
}
load more v
72%

Configuring Trusted Hosts,Resetting The Password,Requesting The Password Reset Link,Routing Requesting The Password Reset Link Resetting The Password

A table must be created to store your application's password reset tokens. The migration for this table is included in the default Laravel application, so you only need to migrate your database to create this table:

php artisan migrate
load more v
65%

Let’s first create a change password form page, and the required route and controller method for the same.,Add the following entry into your route (routes / web.php) file.,Once all of this pass through, we can go ahead and change the password for the user account and redirect him back with the success message.,Validate the new password requirements, new password and confirm password should be same.

Route Entry

Route::get('/changePassword', 'HomeController@showChangePasswordForm');
load more v
75%

I'm currently trying to make change password functionality to my user profile all my inputs are submitted to the controller, but I think there might be something wrong with my function logic maybe? ,Tried dumping request on function and dump was successfully returned. But when wrapping a validation variable around a validation process, the dump was not returned. The request redirects back to the profile page with form data.,If you want to disable any row button on index page, create a policy for the resource and return false on the respective function in my case update(), ,The result should return 422/error message at least into the console when 'Current password' is wrong, not just redirect back to view and when the password is correct then return 200/success message (not implemented yet.) to console or view.

Controller

public
function updatePassword(Request $request) {
   $this - > validate($request, [
      'old_password' => 'required',
      'new_password' => 'required|confirmed',
      'password_confirm' => 'required'
   ]);

   $user = User::find(Auth::id());

   if (!Hash::check($request - > current, $user - > password)) {
      return response() - > json(['errors' => ['current' => ['Current password does not match']]], 422);
   }

   $user - > password = Hash::make($request - > password);
   $user - > save();

   return $user;
}

View

<form method="POST" action="{{ route('update-password') }}">
    @csrf
    @method('PUT')
    <div class="form-group row">
        <label for="old_password" class="col-md-2 col-form-label">{{ __('Current password') }}</label>
        <div class="col-md-6">
            <input id="old_password" name="old_password" type="password" class="form-control" required autofocus>
        </div>
    </div>
    <div class="form-group row">
        <label for="new_password" class="col-md-2 col-form-label">{{ __('New password') }}</label>
        <div class="col-md-6">
            <input id="new_password" name="new_password" type="password" class="form-control" required autofocus>
        </div>
    </div>
    <div class="form-group row">
        <label for="password_confirm" class="col-md-2 col-form-label">{{ __('Confirm password') }}</label>

        <div class="col-md-6">
            <input id="password_confirm" name="password_confirm" type="password" class="form-control" required
                   autofocus>
        </div>
    </div>
    <div class="form-group login-row row mb-0">
        <div class="col-md-8 offset-md-2">
            <button type="submit" class="btn btn-primary">
                {{ __('Submit') }}
            </button>
        </div>
    </div>
</form>
load more v
40%

Now the laravel custom forgot & reset password tutorial example is completed. If you have any question please add your query in comment section. I hope this help for you.,After creating password reset table add below code on it.,Now create to showing reset password option blade file in your views>customauth directory.,Now update below code in your forgot password controller.

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <meta name="csrf-token" content="{{ csrf_token() }}">
  <title>Laravel Ajax CRUD Example Tutorial with - CodingDriver</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
  <link rel="stylesheet" href="https://cdn.datatables.net/r/bs-3.3.5/jq-2.1.4,dt-1.10.8/datatables.min.css"/>

  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  <script src="https://cdn.datatables.net/r/bs-3.3.5/jqc-1.11.3,dt-1.10.8/datatables.min.js"></script>
</head>
  <style>
  .alert-message {
    color: red;
  }
</style>
<body>

<div class="container">
    <h2 style="margin-top: 12px;" class="alert alert-success">Laravel Ajax CRUD Application -
       <a href="https://www.codingdriver.com" target="_blank" >CodingDriver</a>
     </h2><br>
     <div class="row" style="clear: both;margin-top: 18px;">
       <div class="col-12 text-right">
          <a href="/login" class="navbar-brand">
          Laravel Custom Auth
          </a> <button type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation" class="navbar-toggler"><span class="navbar-toggler-icon"></span></button>
          <div id="navbarSupportedContent" class="collapse navbar-collapse">
             <ul class="navbar-nav mr-auto"></ul>
             <ul class="navbar-nav ml-auto">
                <li class="nav-item"><a href="/login" class="nav-link">Login</a></li>
                <li class="nav-item"><a href="/register" class="nav-link">Register</a></li>
             </ul>
          </div>
       </div>
    </div>
    <div class="row">
        <div class="wrapper ">
           <div class="main-panel">
             @yield('content')
           </div>
         </div>
    </div>
</div>
</body>
</html>
load more v
22%

The above 3 commands will change the password of a specified user.,You are right. Changing the password through the route is not recommended. It’s just for quick fixes. Developers should remove the route once they changed the password.,Now, run the URL YOUR_DOMAIN_URL/changepassword in the browser. It will call the route and change the user’s password. The developer must remove this route once the password is changed.,Laravel uses their own Hashing to encrypt the password. For changing the password you need to use the same hashing techniques for encryption then only it works with Laravel authentication.

Enter into the Tinker environment by running the command:

php artisan tinker
load more v

Other "change-undefined" queries related to "Laravel 5.8 change password functionality"