Skip to content

Instantly share code, notes, and snippets.

@felipemarques
Forked from jamesfairhurst/PasswordBroker.php
Last active July 2, 2016 23:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipemarques/0ed1ac7527b65886d0641254d6188334 to your computer and use it in GitHub Desktop.
Save felipemarques/0ed1ac7527b65886d0641254d6188334 to your computer and use it in GitHub Desktop.
Laravel 5.2 Queue Password Reset Email
<?php
namespace App;
use Illuminate\Auth\Passwords\PasswordBroker as IlluminatePasswordBroker;
class PasswordBroker extends IlluminatePasswordBroker
{
/**
* Send the password reset link via e-mail in a queue
*
* @param \Illuminate\Contracts\Auth\CanResetPassword $user
* @param string $token
* @param \Closure|null $callback
* @return int
*/
public function emailResetLink(\Illuminate\Contracts\Auth\CanResetPassword $user, $token, \Closure $callback = null)
{
// We will use the reminder view that was given to the broker to display the
// password reminder e-mail. We'll pass a "token" variable into the views
// so that it may be displayed for an user to click for password reset.
$view = $this->emailView;
// return $this->mailer->queue($view, compact('token', 'user'), function ($m) use ($user, $token, $callback) {
// $m->to($user->getEmailForPasswordReset());
//
// if (! is_null($callback)) {
// call_user_func($callback, $m, $user, $token);
// }
// });
// this works fine
return $this->mailer->queue($view, compact('token', 'user'), function ($m) use ($user, $token) {
$m->to($user->getEmailForPasswordReset());
});
}
}
<?php
namespace App;
use App\PasswordBroker;
use Illuminate\Auth\Passwords\PasswordBrokerManager as IlluminatePasswordBrokerManager;
class PasswordBrokerManager extends IlluminatePasswordBrokerManager
{
/**
* Resolve the given broker.
*
* @param string $name
* @return \App\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']),
$this->app['mailer'],
$config['email']
);
}
}
<?php
namespace App\Providers;
use App\PasswordBrokerManager;
use Illuminate\Auth\Passwords\PasswordResetServiceProvider as IlluminatePasswordResetServiceProvider;
class PasswordResetServiceProvider extends IlluminatePasswordResetServiceProvider
{
/**
* Register the password broker instance.
*
* @return void
*/
protected function registerPasswordBroker()
{
$this->app->singleton('auth.password', function ($app) {
return new PasswordBrokerManager($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