Surfe.be - passive income

Update Password with checking Old Password in Laravel

Update password in Laravel

Changing password is an essential for every software you can take any example from the live applications they all have change password option for there users so that user can easily change there login password.

I assume that you have already implemented the login register and email verification tutorial on your end and now want to have change password feature added to the same application, then let’s get started.

Process to change Current Password

Let me tell you the details on how you should proceed and what should be the use case while changing current password.So basically first thing you will need is the active login users ID or Email so that you can find out old password (hash).

One you have old password or old password’s hash in hand then you can simply ask user to enter current password and validated it with the password that is stored in the database

This is actually a important step to protect user account security, so if any other person finds user account active and visits change password page then he can not modify password until and unless he know the current password. Good so far?

update-password-in-laravel

Step 1 : Create Controller 

php artisan make:controller SettingsController -r

Step 2: Create Route 

we have to create a route for that. So lets make it . To do it i am using resource controller. You can use normal controller . 

Route::resource('backend/settings','SettingsController');

After doing it , we have to create our method inside SettingsController . So go to SettingsController & copy these following code . 

Step 3: Create Required Method

<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\admin\admin;
use Illuminate\Support\Facades\Auth;

class SettingsController extends Controller
{   
    public function __construct()
    {
      $this->middleware('auth:admin'); //If user is not logged in then he can't access this page
    }
  
    public function edit($id)
    {
        $users = admin::find(Auth::user()->id);
        return view('backend.settings.updatepassword',compact('users'));
    }

   
    public function update(Request $request, $id)
    {
        
         $this->validate($request, [

        'oldpassword' => 'required',
        'newpassword' => 'required',
        ]);

     
       
       $hashedPassword = Auth::user()->password;

       if (\Hash::check($request->oldpassword , $hashedPassword )) {
         
         if (!\Hash::check($request->newpassword , $hashedPassword)) {
              
              $users =admin::find(Auth::user()->id);
              $users->password = bcrypt($request->newpassword);
              admin::where( 'id' , Auth::user()->id)->update( array( 'password' =>  $users->password));

              session()->flash('message','password updated successfully'); 
              return redirect()->back();
            }

            else{
                  session()->flash('message','new password can not be the old password!'); 
                  return redirect()->back();
                }

           }

          else{
               session()->flash('message','old password doesnt matched ');
               return redirect()->back();
             }

       }
   
}

After completing this we are in last part. Now we have to create our view part . So let’s create it.

Step 4: Create bldae file

  <form role="form" method="post" action="{{ route('settings.update',$users->id) }}">
              {{csrf_field()}}
              {{method_field('PUT')}}
              <div class="box-body">

                <div class="form-group">
                  <label for="exampleInputEmail1">User Old Password</label>
                  <input type="password" class="form-control" id="exampleInputEmail1" placeholder="Enter old password" name="oldpassword" >
                </div>

                <div class="form-group">
                  <label for="exampleInputEmail1">User New Password</label>
                  <input type="password" class="form-control" id="exampleInputEmail1" placeholder="Enter new password" name="newpassword">
                </div>

                <div class="form-group">
                  <label for="exampleInputEmail1">Password Confirmation</label>
                  <input type="password" class="form-control" id="exampleInputEmail1" placeholder="Enter new password" name="password_confirmation">
                </div>

           </div>
              <!-- /.box-body -->

              <div class="box-footer">
                <button type="submit" class="btn btn-primary">Update Password</button>
              </div>
   </form>

Leave a comments

LET'S SOCIALITE

Recent Tweets

RECOMMENDED POST