I was working on a Project where I had to change the default functionality of Forget and Reset Password to work with username instead on email.
Since Laravel’s out of the box authentication functionality works very well and it does provide easy customization to change your Login to work with any other user property like username / mobile instead of email.
It’s not the case with Forget/Reset Password functionality. This functionality code is highly depended on user’s email and to change it I had to modify and extend couple of classes in my project.
If you are facing similar issue and you are looking to modify the Reset Password Functionality in Laravel. You need to extend the Illuminate\Auth\Passwords\PasswordBroker
Class from Laravel’s Authentication module.
But Even If you Extend this Class in your Project, It won’t work because PasswordBroker Class is referred by PasswordBrokerManager which is registered in ServiceProvider. So there are a couple of steps involved.
Use coupon 5balloons on this Cloudways Affiliate URL to get special discount.
1. Create a CustomPasswordResetServiceProvider inside App\Providers
You need to create a new CustomPasswordResetServiceProvider
class which we will use to replace the Default PasswordResetServiceProvider
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Services\CustomPasswordBrokerManager;
class CustomPasswordResetServiceProvider extends ServiceProvider{
protected $defer = true;
public function register()
{
$this->registerPasswordBrokerManager();
}
protected function registerPasswordBrokerManager()
{
$this->app->singleton('auth.password', function ($app) {
return new CustomPasswordBrokerManager($app);
});
}
public function provides()
{
return ['auth.password'];
}
}
2. Replace Service Provider in app/config.php
Next, we need to replace the newly created ServiceProvider in our app/config.php
Open you app/config.php file and comment out PasswordResetServiceProvider and add new the ServiceProvider class.
//Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
App\Providers\CustomPasswordResetServiceProvider::class,
3. Create new CustomPasswordBrokerManager class
Create a new class CustomPasswordBrokerManager and under directory App/Services and copy all the contents of PasswordBrokerManager which is located at Illuminate\Auth\Passwords\PasswordBrokerManager.php
Then modified the function resolve to return an instance of my CustomPasswordProvider class
protected function resolve($name)
{
$config = $this->getConfig($name);
if (is_null($config)) {
throw new InvalidArgumentException("Password resetter [{$name}] is not defined.");
}
return new CustomPasswordBroker(
$this->createTokenRepository($config),
$this->app['auth']->createUserProvider($config['provider'])
);
}
4. Create CustomPasswordBroker
Finally, You can now create your new CustomPasswordBroker class under App/Services directory which extends the default PasswordBroker class located at Illuminate\Auth\Passwords\PasswordBroker
use Illuminate\Auth\Passwords\PasswordBroker as BasePasswordBroker;
class CustomPasswordBroker extends BasePasswordBroker
{
// override the functions that you need here
}
Now, you can override the functions that you need. Mostly you will need to start your modification with function sendResetLink
and then you can take it ahead from there.