Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vanquyet94/ff0d84ffcdd0d30a5426dbcf5d17e1b6 to your computer and use it in GitHub Desktop.
Save vanquyet94/ff0d84ffcdd0d30a5426dbcf5d17e1b6 to your computer and use it in GitHub Desktop.
Laravel Custom Reset Password Token
<?php
namespace App\Providers\Passwords;
use Illuminate\Auth\Passwords\DatabaseTokenRepository;
class MyDatabaseTokenRepository extends DatabaseTokenRepository
{
/**
* [Override]
* Create a new token for the user.
*
* @return string
* @throws \Exception
*/
public function createNewToken()
{
// Custom Token
return sprintf("%06d", mt_rand(1, 999999));
}
}
<?php
namespace App\Providers\Passwords;
use Closure;
use Illuminate\Auth\Passwords\TokenRepositoryInterface;
use Str;
use Illuminate\Auth\Passwords\PasswordBrokerManager;
class MyPasswordBrokerManager extends PasswordBrokerManager
{
/**
* [Override]
* Create a token repository instance based on the given configuration.
*
* @param array $config
*
* @return TokenRepositoryInterface
*/
protected function createTokenRepository(array $config)
{
$key = $this->app['config']['app.key'];
if (Str::startsWith($key, 'base64:')) {
$key = base64_decode(substr($key, 7));
}
$connection = $config['connection'] ?? null;
return new MyDatabaseTokenRepository(
$this->app['db']->connection($connection),
$this->app['hash'],
$config['table'],
$key,
$config['expire']
);
}
/**
* Send a password reset link to a user.
*
* @param array $credentials
* @return string
*/
public function sendResetLink(array $credentials)
{
return parent::sendResetLink($credentials);
}
/**
* Reset the password for the given token.
*
* @param array $credentials
* @param \Closure $callback
* @return mixed
*/
public function reset(array $credentials, Closure $callback)
{
return parent::reset($credentials, $callback);
}
/**
* Set a custom password validator.
*
* @param \Closure $callback
* @return void
*/
public function validator(Closure $callback)
{
parent::validator($callback);
}
/**
* Determine if the passwords match for the request.
*
* @param array $credentials
* @return bool
*/
public function validateNewPassword(array $credentials)
{
return parent::validateNewPassword($credentials);
}
}
<?php
namespace App\Providers\Passwords;
use Illuminate\Auth\Passwords\PasswordResetServiceProvider;
class MyPasswordResetServiceProvider extends PasswordResetServiceProvider
{
/**
* [Override]
* Register the password broker instance.
*
* @return void
*/
protected function registerPasswordBroker()
{
$this->app->singleton('auth.password', function ($app) {
return new MyPasswordBrokerManager($app);
});
$this->app->bind('auth.password.broker', function ($app) {
return $app->make('auth.password')->broker();
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment