0

I want to modify Password Facade by adding methods to DatabaseTokenRepository and PasswordBroker

class DatabaseTokenRepository implements TokenRepositoryInterface
{
    /*I want to add the method, reference to the exists() method*/
    public function getRecord(CanResetPasswordContract $user)
    {
        return (array) $this->getTable()->where(
            'email', $user->getEmailForPasswordReset()
        )->first();
    }
}
class PasswordBroker implements PasswordBrokerContract
{
    /*I want to add the method*/
    public function getTokenRecord(CanResetPasswordContract $user)
    {
        return $this->tokens->getRecord($user);
    }
}

So I can use Password::getTokenRecord($user) to get the database record in the password_resets table.

I believe to use Password Facade, the code is more consistency, instead to use DB:table('password_resets')->where('email', $user->email)->first().

Is there a way to register these method? Or macro? Or classes which extend DatabaseTokenRepository and PasswordBroker, and then tell the Password Facade to use my extended classes?

Any suggestion? Thank you!

Autodesk
  • 631
  • 8
  • 27

1 Answers1

0

Thanks to the similar question, I solve the problem!

Here are my codes:

  1. the custom method getTokenRecord() for me to use Password::getTokenRecord($user)
namespace App\Facades;

use Illuminate\Auth\Passwords\PasswordBroker as Broker;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class PasswordBroker extends Broker
{
    public function getTokenRecord(CanResetPasswordContract $user)
    {
        return $this->tokens->getRecord($user);
    }
}
  1. the getRecord() which is called from the getTokenRecord() method
namespace App\Repositories;

use App\Models\Member;
use Illuminate\Auth\Passwords\DatabaseTokenRepository as DatabaseToken;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class DatabaseTokenRepository extends DatabaseToken
{
    public function getRecord(CanResetPasswordContract $user)
    {
        return $this->getTable()->where(
            'email',
            $user->getEmailForPasswordReset()
        )->first();
    }
}

  1. the custom PasswordBrokerManager to resolve my custom PasswordBroker and DatabaseTokenRepository
<?php

namespace App\Facades;

use App\Facades\PasswordBroker;
use App\Repositories\DatabaseTokenRepository;
use Illuminate\Auth\Passwords\PasswordBrokerManager as Manager;
use InvalidArgumentException;

class PasswordBrokerManager extends Manager
{
    /**
     * Resolve the given broker.
     *
     * @param  string  $name
     * @return \Illuminate\Contracts\Auth\PasswordBroker
     *
     * @throws \InvalidArgumentException
     */
    protected function resolve($name)
    {
        $config = $this->getConfig($name);

        if (is_null($config)) {
            throw new InvalidArgumentException("Password resetter [{$name}] is not defined.");
        }

        // 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 PasswordBroker(
            $this->createTokenRepository($config),
            $this->app['auth']->createUserProvider($config['provider'] ?? null)
        );
    }

    /**
     * Create a token repository instance based on the given configuration.
     *
     * @param  array  $config
     * @return \Illuminate\Auth\Passwords\TokenRepositoryInterface
     */
    protected function createTokenRepository(array $config)
    {
        $key = $this->app['config']['app.key'];

        if (str_starts_with($key, 'base64:')) {
            $key = base64_decode(substr($key, 7));
        }

        $connection = $config['connection'] ?? null;

        return new DatabaseTokenRepository(
            $this->app['db']->connection($connection),
            $this->app['hash'],
            $config['table'],
            $key,
            $config['expire'],
            $config['throttle'] ?? 0
        );
    }
}

  1. register my custom Password Facade
<?php

namespace App\Providers;

use App\Facades\PasswordBrokerManager;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Support\DeferrableProvider;

class PasswordResetServiceProvider extends ServiceProvider implements DeferrableProvider
{

    public function register()
    {
        $this->registerPasswordBrokerManager();
    }

    protected function registerPasswordBrokerManager()
    {
        $this->app->singleton('auth.password', function ($app) {
            return new PasswordBrokerManager($app);
        });
    }

    public function provides()
    {
        return ['auth.password'];
    }
}

  1. comment out the origin PasswordResetServiceProvider::class from config/app.php, and add my custom class
'providers' => [
    // Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
    App\Providers\PasswordResetServiceProvider::class,
]

Autodesk
  • 631
  • 8
  • 27