MGS
MGS

Reputation: 103

password reset table column name override laravel 5

Hello friends i have modified password reset table column name 'created_at' instead of 'created'. if i am changing column name 'created' on migration, But i am getting the error column not found 'created_at'.

 \vendor\laravel\framework\src\Illuminate\Auth\Passwords\DatabaseTokenRepository.php
 protected function getPayload($email, $token)
{
    return ['email' => $email, 'token' => $token, 'created_at' => new Carbon];
}

this is the file coming from column name 'created_at' where i can override this function please suggest me..

Upvotes: 3

Views: 2202

Answers (2)

Hashmat Waziri
Hashmat Waziri

Reputation: 477

Solved: I applied the 5.2 steps in my Laravel 5.4 project:

ForgotPasswordHelperRepository.php

 <?PHP


 namespace App\Helpers;


use Carbon\Carbon;

 class ForgotPasswordHelperRepository extends \Illuminate\Auth\Passwords\DatabaseTokenRepository
   {

/**
 * Build the record payload for the table.
 * I wanted to add an extra column organization_id
 * organizationId() is a helper method I created
 * @param string $email
 * @param string $token
 * @return array
 */
protected function getPayload($email, $token)
{
    return ['email' => $email, 'token' => $this->hasher->make($token), 'created_at' => new Carbon,'organization_id' => organizationId()];
}

  }

PasswordBrokerManagerHeler.php

<?PHP


namespace App\Helpers;


use Closure;
use Illuminate\Auth\Passwords\DatabaseTokenRepository;
use Illuminate\Auth\Passwords\PasswordBroker;
use Illuminate\Support\Str;

 class PasswordBrokerManagerHelper extends \Illuminate\Auth\Passwords\PasswordBrokerManager
{

/**
 * @inheritDoc
 */
public function sendResetLink(array $credentials)
{
    // TODO: Implement sendResetLink() method.
}

/**
 * @inheritDoc
 */
public function reset(array $credentials, Closure $callback)
{
    // TODO: Implement reset() method.
}

/**
 * @inheritDoc
 */
public function validator(Closure $callback)
{
    // TODO: Implement validator() method.
}

/**
 * @inheritDoc
 */
public function validateNewPassword(array $credentials)
{
    // TODO: Implement validateNewPassword() method.
}

/**
 * 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'])
    );
}



protected function createTokenRepository(array $config)
{
    $key = $this->app['config']['app.key'];

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

    $connection = isset($config['connection']) ? $config['connection'] : null;

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

Next, just copy and paste the following in the AppServiceProvider@register method

  $this->app->singleton('auth.password', function ($app) { return new    PasswordBrokerManagerHelper($app); });
  $this->app->bind('auth.password.broker', function ($app) { return $app->make('auth.password')->broker(); });

Upvotes: 0

thefallen
thefallen

Reputation: 9749

I think I've found a way to do this without touching the vendor directory.

For Laravel 5.2

  1. Create a class that extends Illuminate\Auth\Passwords\DatabaseTokenRepository
  2. Overwrite the getPayload() method there
  3. Create a class that extends Illuminate\Auth\Passwords\PasswordBrokerManager
  4. Overwrite the resolve() method to return a new PasswordBroker with your token repository from step 1
  5. Open config/app.php and comment out PasswordResetServiceProvider from the providers array
  6. In your app service provider register an instance of your password broker manager from step 3

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

    $this->app->bind('auth.password.broker', function ($app) { return $app->make('auth.password')->broker(); });

For Lravel 5

  1. Create a class that extends Illuminate\Auth\Passwords\DatabaseTokenRepository
  2. Overwrite the getPayload() method there
  3. Create a class that extends Illuminate\Auth\Passwords\PasswordResetServiceProvider
  4. Overwrite the registerTokenRepository() to return your repository from step 1
  5. Open config/app.php and comment out PasswordResetServiceProvider from the providers array
  6. Add your provider from step 3 to the providers array

Please note that I haven't tested this, but it should work on theory.

Upvotes: 4

Related Questions