How to override reset and validatePasswordWithDefaults in PasswordBroker Laravel 5

I'm really new to this framework and it seems so magical to me. I can't even find where it is calling the reset () function in the route and controller. but I know he called in front of the controller after browsing the internet for a whole day.

Here's the problem, I tested overriding the reset function and the validatePasswordWithDefaults function in PasswordBroker

I do this, extends PasswordBroker, but it looks like I need to completely migrate the entire feature in Illuminate \ Auth \ Passwords \ PasswordBroker to my App \ Services \ PasswordBroker yet I'll remove the error:

Target [Illuminate\Contracts\Auth\UserProvider] is not instantiable

      

My example code:

Custom PasswordServiceProviders that bind my PasswordBroker to PasswordBroker highlighting:

<?php namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class PasswordResetServiceProvider extends ServiceProvider {

/**
 * Bootstrap the application services.
 *
 * @return void
 */
public function boot()
{
    //
}

/**
 * Register the application services.
 *
 * @return void
 */
public function register()
{
    //
    $this->app->bind(
        'Illuminate\Contracts\Auth\PasswordBroker','App\Services\PasswordBroker'
        );


}

}

Custom PasswordBroker:
<?php
    namespace App\Services;


    use Illuminate\Contracts\Auth\UserProvider;
    use Illuminate\Auth\Passwords\TokenRepositoryInterface;
    use Illuminate\Auth\Passwords\PasswordBroker as BasePasswordBroker;
    use Illuminate\Contracts\Auth\PasswordBroker as ContractPasswordBroker;

    use Closure;

    class PasswordBroker extends BasePasswordBroker
    {

        public function reset(array $credentials, Closure $callback)
        {
            dd($callback);
            $user = $this->validateReset($credentials);

            if ( ! $user instanceof CanResetPasswordContract)
            {
                return $user;
            }

            $pass = $credentials['password'];

            call_user_func($callback, $user, $pass);

            $this->tokens->delete($credentials['token']);

            return PasswordBrokerContract::PASSWORD_RESET;
        }

        protected function validatePasswordWithDefaults(array $credentials)
        {
            list($password, $confirm) = [
                $credentials['password'], $credentials['password_confirmation'],
            ];

            return $password === $confirm && mb_strlen($password) >= 4;
        }
    }
    ?>

      

+3


source to share


1 answer


It's not easy, in my opinion, the framework shouldn't send an email that deep into the code and provide a way to override it from the controller.

I had to override sending email because I needed to use the Mandrill api, so I had to send additional headers the very moment the mail is being sent. This is what I did:



  • Create a provider class in App \ Providers \ PasswordResetServiceProvider. I copied the underlying infrastructure provider (Illuminate \ Auth \ Passwords \ PasswordResetServiceProvider), but I had to make some small changes to the registration order and then along the way to get the token service. Also you need to specify where your PasswordBroker is located (in my case it is \ App \ Services \ PasswordBroker)

     use Illuminate\Support\ServiceProvider;
     use Illuminate\Auth\Passwords\DatabaseTokenRepository as DbRepository;
    
     class PasswordResetServiceProvider extends ServiceProvider {
    
    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    protected $defer = true;
    
    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->registerTokenRepository();
        $this->registerPasswordBroker();
    }
    
    /**
     * Register the password broker instance.
     *
     * @return void
     */
    protected function registerPasswordBroker()
    {
        return $this->app->singleton('auth.password', function($app)
        {
            // The password token repository is responsible for storing the email addresses
            // and password reset tokens. It will be used to verify the tokens are valid
            // for the given e-mail addresses. We will resolve an implementation here.
            $tokens = $app['auth.password.tokens'];
    
            $users = $app['auth']->driver()->getProvider();
    
            $view = $app['config']['auth.password.email'];
    
            // The password broker uses a token repository to validate tokens and send user
            // password e-mails, as well as validating that password reset process as an
            // aggregate service of sorts providing a convenient interface for resets.
            return new \App\Services\PasswordBroker(
                $tokens, $users, $app['mailer'], $view
            );
        });
    }
    
    /**
     * Register the token repository implementation.
     *
     * @return void
     */
    protected function registerTokenRepository()
    {
        $this->app->singleton('auth.password.tokens', function($app)
        {
            $connection = $app['db']->connection();
    
            // The database token repository is an implementation of the token repository
            // interface, and is responsible for the actual storing of auth tokens and
            // their e-mail addresses. We will inject this table and hash key to it.
            $table = $app['config']['auth.password.table'];
    
            $key = $app['config']['app.key'];
    
            $expire = $app['config']->get('auth.password.expire', 60);
    
            return new DbRepository($connection, $table, $key, $expire);
        });
    }
    
    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides()
    {
        return ['auth.password', 'auth.password.tokens'];
    }
    
          

    }

    1. Create class \ App \ Services \ PasswordBroker, there you can override emailResetLink (), there is no secret in this step.

    2. Register a new provider in the provider array from config \ app.php (App \ Providers \ PasswordResetServiceProvider). Comment out the default (Illuminate \ Auth \ Passwords \ PasswordResetServiceProvider)

+3


source







All Articles