Thanks to the similar question, I solve the problem!
Here are my codes:
- 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);
}
}
- 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();
}
}
- 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
);
}
}
- 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'];
}
}
- 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,
]