In this article, we will be discussing how to implement password resets on multiple tables in laravel.
Multiple tables in this context means that you have three different users in your application namely;
Their information including emails and passwords are uniquely stored in their respective database tables. If you have this scenario in a laravel application how do you reset the individual password or send password reset email to a particular user (either players, admin or coache) using the default Laravel Auth Scaffolding since it supports mainly the User model by default.
Before we begin solving the problem, let me describe a real life scenario I encountered recently that led to the development of this article. I was recently added to an existing codebase that I don’t have much control to redesign the architecture, I just needed to build on top of what is existing.
In this company, we render services to three different people for example;
Everything about the business logic is working very fine but my task was to implement PasswordReset for Coaches and Admins since Players/Users was already done using the default Laravel Auth Scaffolding.
I know what you’re thinking, let me guess, you will say ah so easy, just create a new controller called it anything and write your own password reset and/or password reset email logic in there… uhmmm :) not so fast. What I really wanted is to still use the default Laravel Auth scaffolding controllers for these three users, you can say I just love Laravel Auth logics.
If you understand the scenario, let’s move on, if not read again and again and God help you.
After series of stack overflow, google and YouTube searches, I was introduced to Laravel Brokers as the final and working solution.
So what is Brokers and how to implement it to solve our problem
According to Laravel, password Broker is used to rest passwords on multiple user tables if configured properly. You can customize the included ForgetPasswordController and ResetPasswordController to use the broker of your choice by overriding the broker method. You can read more here or take a look at theBroker class here.
BTW, that’s exactly what I wanted, if that’s what you want too, then keep reading, let’s see how I configured it, use it inside Forget and Reset Password Controller and also how I use it dynamically in my blade template and routes.
Configuring the Brokers
The construction of PasswordBrokers take in two arguments:
The TokenRespositoryInterface is an interface with create , exist , delete , deleteExpired methods. They kind of enforce a strict rule that any model you want to use PasswordBroker on should be able to perform/override these methods.
The UserProvider class is a provider that will be used to retrieve , validate , retrieveCredentials of the user you want to Reset the Password on.
Now that the theories are out, let’s move to configuring our Broker;
Inside my auth.php, I added the following codes:
Update the guards array.
Update the providers array.
Update the passwords array
That’s all the configuration needed.
Overriding the default Broker in our Controller
After configuring our password broker, it’s time to use it inside the ForgotPasswordController and ResetPasswordController already generated by Laravel Auth Scaffolding.
php artisan auth:ui
Open the ForgotPasswordController and ResetPasswordController and add the following lines of code
Very important note: the value of user-type should be same as the one you define in the configuration guards arrays.
That said, also override the ShowResetForm method of the ResetPasswordController to
And ShowLinkRequestForm( ) of ForgotPasswordController to
That was just so I can pass the user-type around every request until it gets to the broker.
Sending user-type on Blade Template
I opened my login form for each of the users and added an extra query parameter user-type and set it to the current logic that wants to rest the password.
For example, In Coaches Login form, i added extra param to the URL.
Now open email.blade.php and reset.blade.php inside views/auth/passwords/ and add an hidden input field like so
Next, open web.php and add the following routes for each of the ForgotPassword link for each users
Sending custom password reset mail
Open each of your model, i.e coach.php, player.phpand admin.php and add the following code
NB: user-type must be the same as before.
Create the new notification object.
php artisan make:notification customPasswordResetNotification
Open the newly created notification and pass in the following code
At the constructor,
At the toMail method
NB: make sure to include the user-type as a query parameter.
If you have an easier way to pass the user-type around till it gets to the Password Broker, hmmm, i will be happy :) to merge your Pull Request here.
WOW! If you are here, congratulations, that was a long one. I understand that there could be a better, easier and efficient way to do this. Please drop it in the comment section but for now this works and save my ass at work. Hoping to hear your suggestions, improvements etc.
Please share, you could save someone’s job.
There you have it, if you have any further contributions, questions or feedback, please drop it. Also don’t forget to CLAP if you find the article useful,
I hope you learnt something new with Password Broker in Laravel. The full code is on GitHub, get it now.
Thank you for reading my article and my editor Owanate Amachree. Here at my blog or medium I regularly write about backend development, digital marketing and content management system. To read my future posts simply join my publication or click ‘Follow’ Also feel free to connect with me via Twitter, Facebook, Instagram.
If you are interested in backend development (or you’re internet enthusiast) both (Mobile | Web | Desktop) videos subscribe to my Youtube channel, we will be posting a collection of help full tutorials and guides like this one for artisans.
If you enjoy this post, make sure to let us know and share it with your friends and subscribe to my growing channel.
Sharing is caring.