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.
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.
@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);
}
}
<?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
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));
}
@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.