Search code examples
phplaravellaravel-5illuminate-container

Reuse password broker logic to send activation email


I've got a simple CRUD application in which is possible to add users, through a form, the ideal flow is

  • an admin fills the form with user name and password
  • an email is sent to the user with a link to set the password
  • the user is logged in

Now at the end of the day what I really want is that the PasswordBroker does, but instead to send the emails.passsowrd view I want a different one, and the rest of the logic can be the same

Is there an easy way so create a valid isntace of the passwordBroker and passing a different view?

the property emailWiew is protected so I cannot override it, I tried to create a new instance out of the IoC

$tokens = $this->app['auth.password.tokens'];

$users = $this->app['auth']->driver()->getProvider();

$view = "emails.createPassword";

$password =  new PasswordBroker(
    $tokens, $users, $this->app['mailer'], $view
);
dd($users->retrieveByCredentials(["[email protected]"])); //i get null and the user email is valid

$response = $password->sendResetLink(["[email protected]"], function (Message $message) {
    $message->subject("Set your password");
});
dd($response); // I get "passwords.user" wich is an error

but when I pass the email address I get an invalid user error

any idea?


Solution

  • The problem is that you're not providing the key to retrieve the credentials, so it's trying to get a field in the users table by the name of "0". use the following and it will work:

    $response = $password->sendResetLink(
        ["email" => "[email protected]"], 
        function (Message $message) {
            $message->subject("Set your password");
    });