Skip to content

Instantly share code, notes, and snippets.

@JT501
Last active May 4, 2024 01:31
Show Gist options
  • Save JT501/1f800576d7ad23b3d29b378cf7051403 to your computer and use it in GitHub Desktop.
Save JT501/1f800576d7ad23b3d29b378cf7051403 to your computer and use it in GitHub Desktop.

Revisions

  1. JT501 revised this gist Sep 3, 2019. 3 changed files with 1 addition and 18 deletions.
    7 changes: 1 addition & 6 deletions MyDatabaseTokenRepository.php
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,4 @@
    <?php
    /**
    * Created by PhpStorm.
    * User: Kiu Cheong
    * Date: 13/2/2019
    * Time: 3:56 PM
    */

    namespace App\Providers\Passwords;

    @@ -21,6 +15,7 @@ class MyDatabaseTokenRepository extends DatabaseTokenRepository
    */
    public function createNewToken()
    {
    // Custom Token
    return sprintf("%06d", mt_rand(1, 999999));
    }
    }
    6 changes: 0 additions & 6 deletions MyPasswordBrokerManager.php
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,4 @@
    <?php
    /**
    * Created by PhpStorm.
    * User: Kiu Cheong
    * Date: 13/2/2019
    * Time: 4:09 PM
    */

    namespace App\Providers\Passwords;

    6 changes: 0 additions & 6 deletions MyPasswordResetServiceProvider.php
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,4 @@
    <?php
    /**
    * Created by PhpStorm.
    * User: Kiu Cheong
    * Date: 13/2/2019
    * Time: 4:17 PM
    */

    namespace App\Providers\Passwords;

  2. JT501 created this gist Sep 3, 2019.
    26 changes: 26 additions & 0 deletions MyDatabaseTokenRepository.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    <?php
    /**
    * Created by PhpStorm.
    * User: Kiu Cheong
    * Date: 13/2/2019
    * Time: 3:56 PM
    */

    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()
    {
    return sprintf("%06d", mt_rand(1, 999999));
    }
    }
    90 changes: 90 additions & 0 deletions MyPasswordBrokerManager.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,90 @@
    <?php
    /**
    * Created by PhpStorm.
    * User: Kiu Cheong
    * Date: 13/2/2019
    * Time: 4:09 PM
    */

    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);
    }
    }
    31 changes: 31 additions & 0 deletions MyPasswordResetServiceProvider.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    <?php
    /**
    * Created by PhpStorm.
    * User: Kiu Cheong
    * Date: 13/2/2019
    * Time: 4:17 PM
    */

    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();
    });
    }
    }