Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

gustav1105's avatar

Where to add logic after reset password.

In laravel 5.4 where would I put some logic after the reset password has been completed, I have a table that needs to update a field when a user has successfully reset his password.

The problem is the ResetPasswords is in the vendor folder, and if I add the logic there it breaks.

Where will I be able to put this logic after the reset password is completed and the user is authorized just before he is redirected to /home.

0 likes
10 replies
somnathsah's avatar

There is controller with the following path

app\Http\Controllers\Auth\ResetPasswordController.php

where you can write your custom logic and if you need you can override these methods of vendor\laravel\framework\src\Illuminate\Foundation\Auth\ResetsPasswords.php traits.

gustav1105's avatar

@somnathsah Tried it does not work takes no affect, here is my code it does not work

<?php

namespace App\Http\Controllers\Auth;

use App\Role;
use App\Monitor;
use Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;

class ResetPasswordController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Password Reset Controller
    |--------------------------------------------------------------------------
    |
    | This controller is responsible for handling password reset requests
    | and uses a simple trait to include this behavior. You're free to
    | explore this trait and override any methods you wish to tweak.
    |
    */



    use ResetsPasswords;

    /**
     * Where to redirect users after resetting their password.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    public function loginCounter() 
    {


        $user = Auth::user();

        $monitor = new Monitor();

        $monitor->login_counter = 1;

        $user->monitor()->save($monitor);

        $role = Role::where('name', 'user');

        $user->attachRole($role);
    }



}

somnathsah's avatar

Change your code to as below

<?php

namespace App\Http\Controllers\Auth;

use App\Role;
use App\Monitor;
use Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;

class ResetPasswordController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Password Reset Controller
    |--------------------------------------------------------------------------
    |
    | This controller is responsible for handling password reset requests
    | and uses a simple trait to include this behavior. You're free to
    | explore this trait and override any methods you wish to tweak.
    |
    */



   use ResetsPasswords {
        reset as protected resetPassword;
    }

    /**
     * Where to redirect users after resetting their password.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    public function loginCounter(Request $request) 
    {

    $this->resetPassword($request);

        $user = Auth::user();

        $monitor = new Monitor();

        $monitor->login_counter = 1;

        $user->monitor()->save($monitor);

        $role = Role::where('name', 'user');

        $user->attachRole($role);
    }

}

and changes in reset.blade.php, change form action to

action="{{ url('/reset_password') }}"

and create a route

Route::get('/reset_password', 'Auth\ResetPasswordController@loginCounter');
1 like
gustav1105's avatar

I ended up putting the logic in my home controller and checking if the relationship to monitor is Null.

If it is it does the logic.

It is not the best solution, In fact a lousy solution but I cannot for the life of me get the above to work.

@somnathsah I get a error about the trait when trying your solution...

Will give it some more thought tommorrow.

Thanks.

somnathsah's avatar

@gustav1105 I will be away from system for two days, after that I try something new and let you know.

Also please post the error you are getting.

Snapey's avatar

you are counting logins in the reset password controller?

Snapey's avatar

duplicate this code from the trait and insert a call to your function where shown

    /**
     * Get the response for a successful password reset.
     *
     * @param  string  $response
     * @return \Illuminate\Http\RedirectResponse
     */
    protected function sendResetResponse($response)
    {
        // Put a call to your function here, e.g.
        $this->logincounter();

        return redirect($this->redirectPath())
                            ->with('status', trans($response));
    }
gustav1105's avatar

@Snapey No I do not have the logic for the counting in the reset controller, In fact I did not put any of it in my reset controller, ended up putting the logic to new up inmy home controller if the table relationship is null between user and login counter,

Then the logic for counting the logins is in my AuthenticatesUser Trait, (the trait shipped with make:auth) in illuminate folder.

It works quite well! but what doe you think?

Should I rethink my strategy?

Snapey's avatar

Check the title of your question.....

Please or to participate in this conversation.