Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I override / add to the Password Broker in Laravel?

Tags:

php

laravel-5

Using Laravel 5.2

I need to overwrite a couple of pieces of functionality / add some new stuff to the password reset process. I've know I can overwrite the functionality in Illuminate\Foundation\Auth\ResetsPasswords using the Password Controller that comes out of the tin.

I also need to make changes in Illuminate\Contracts\Auth\PasswordBroker. My goal is to have an invitation email for setting an initial password that uses the existing password reset functionality. The only problem is in the function sendResetLink.

This function gets the password token and sends the email via the reset password email view. I still need the password reset functionality, but I can't have an invitation email with 'reset your password' verbiage.

How can I do this? I think I can duplicate and tweak the functionality on the reset classes to use an invite view for the email, but I can't figure out how to extend the broker class to make this work.

like image 997
TH1981 Avatar asked Feb 20 '16 06:02

TH1981


People also ask

What is passwordbroker in Laravel?

The most common usage of PasswordBroker is to send the password reset link to users' emails by chaining it with sendResetLink () method. In the background, it uses the provider specified by given broker in auth.php. What magical about it is that at the end you will end up with User model or other model extending Authenticable

How to reset user passwords in Laravel using migrations?

So, all you need to do is run your database migrations: Laravel includes Auth\ForgotPasswordController and Auth\ResetPasswordController classes that contains the logic necessary to e-mail password reset links and reset user passwords. All of the routes needed to perform password resets may be generated using the make:auth Artisan command:

What is passwordbroker and how to use it?

The most common usage of PasswordBroker is to send the password reset link to users' emails by chaining it with sendResetLink () method. In the background, it uses the provider specified by given broker in auth.php.

Should I use Swiftmailer or PHPMailer in Laravel?

The better thing to do would be to tell Laravel to use PHPMailer instead of SwiftMailer whenever it needs to mail anything. This way, all the Laravel code stays the same.


1 Answers

I was struggling with the same thing. I added a setEmailView() method to my custom PasswordBroker and called that just before sending the resetlink in AuthController, but that broke the regular password reset function.
So, I scrapped all that and went for a much simpler approach. In AuthController.php, inject a variable into the password template.

view()->composer('auth.emails.password', function($view) {
    $view->with(['register'=>true]);
});
Password::sendResetLink(['email'=>$data['email']], function($message) {
    $message->subject('Registration Email');
});

Then, in the template auth/emails/password.blade.php

@if ( isset($register) )
    Whatever you want to say in registration Email.
@else
    Normal password reset Email here.
@endif
like image 194
GsSherman Avatar answered Oct 18 '22 18:10

GsSherman