Skip to content

Instantly share code, notes, and snippets.

@jamesfairhurst
Created May 3, 2016 08:00
Show Gist options
  • Save jamesfairhurst/a30f034f6aeef45fe32f17e5588c1adf to your computer and use it in GitHub Desktop.
Save jamesfairhurst/a30f034f6aeef45fe32f17e5588c1adf 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);
}
});
}
}
<?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();
});
}
}
@felipemarques
Copy link

i get this error:

Serialization of closure failed: Serialization of closure failed: Serialization of 'Closure' is not allowed

Can you help me ?

@felipemarques
Copy link

felipemarques commented Jul 2, 2016

@jamesfairhurst

    // this works fine
    return $this->mailer->queue($view, compact('token', 'user'), function ($m) use ($user, $token) {
        $m->to($user->getEmailForPasswordReset());
    });

its wrong?

@MacWorks
Copy link

@felipemarques

Nothing wrong with that if you don't need to call a callback.

@Shuvo2220
Copy link

Where should put these files?

@franciscocorrales
Copy link

Awesome.
Checked on Laravel 5.3.
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment