Password Brokers: Reset Passwords on Multiple Tables in Laravel
Tags: programming, softwaredevelopment, coding, webdev
Published: 2020-02-01 15:55:54

---
title: Password Brokers: Reset Passwords on Multiple Tables in Laravel
published: true
date: 2020-02-01 13:02:43 UTC
tags: programming,software-development,coding,web-development
canonical_url: https://medium.com/backenders-club/password-brokers-reset-passwords-on-multiple-tables-in-laravel-73068542925c
---

![](https://cdn-images-1.medium.com/max/1024/1*DT9Ao2XeHQwbjYEnfUdSUw.png)<figcaption>implement password resets on multiple tables in laravel</figcaption>

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;

1. Players/users
2. Admin
3. Coaches

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;

1. Coaches
2. Players/Users
3. Admins.

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.

**LARAVEL BROKERS**

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](https://laravel.com/docs/5.8/passwords#password-customization) or [take a look at the](https://laravel.com/api/5.6/Illuminate/Auth/Passwords/PasswordBroker.html)[Broker class here](https://laravel.com/api/5.6/Illuminate/Auth/Passwords/PasswordBroker.html).

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:

1. TokenRespositoryInterface
2. UserProvider

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.**

![](https://cdn-images-1.medium.com/max/1024/1*taWAGQb4GxBuZc94as8kFg.png)<figcaption>Guards array</figcaption>

**Update the**  **providers array.**

![](https://cdn-images-1.medium.com/max/974/1*crAiyzjfvckm0Eewggi8Xg.png)

**Update the**  **passwords array**

![](https://cdn-images-1.medium.com/max/992/1*DnL9806Phua6OxptnJLSJA.png)

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

![](https://cdn-images-1.medium.com/max/1024/1*tXTZzBBy7LMRZKgivn6kyg.png)

**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

![](https://cdn-images-1.medium.com/max/1024/1*xLkt1SU0pbwgp9bT9dTS3g.png)

And ShowLinkRequestForm( ) of ForgotPasswordController to

![](https://cdn-images-1.medium.com/max/1024/1*qMV5gWlSAiOUo6dwAey7iQ.png)

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.

![](https://cdn-images-1.medium.com/max/1024/1*H0i35KlFe0b3gB-SZlca_w.png)

Now open email.blade.php and reset.blade.php inside views/auth/passwords/ and add an hidden input field like so

![](https://cdn-images-1.medium.com/max/1024/1*iKVOV200PwqrOvcm8wJajw.png)

Next, open web.php and add the following routes for each of the ForgotPassword link for each users

![](https://cdn-images-1.medium.com/max/1024/1*cZH-aWQdzbOPdWW3D26yhw.png)

**Sending custom password reset mail**

Open each of your model, i.e coach.php, player.phpand admin.php and add the following code

![](https://cdn-images-1.medium.com/max/1024/1*hPlbUfzB-BF7pMrJpiq-rA.png)

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,

![](https://cdn-images-1.medium.com/max/958/1*Qgu5izJ9kROoIHZ_zXm49A.png)

At the toMail method

![](https://cdn-images-1.medium.com/max/1024/1*7v5hNjASNwhNlVRgSxGraQ.png)

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.](https://github.com/Kaperskyguru/password-broker-example)

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.**

**Conclusion**

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](https://github.com/Kaperskyguru/password-broker-example).

Thank you for reading my article and my editor [Owanate Amachree](https://medium.com/u/741595afc821). Here at my [blog](https://bit.ly/multimegablog) or [medium](https://bit.ly/mymediumprofile) I regularly write about backend development, digital marketing and content management system. To read my future posts simply [join my publication](https://bit.ly/mymediumpublication) or click ‘Follow’ Also feel free to connect with me via [Twitter](http://twitter.com/kaperskyguru), [Facebook](https://facebook.com/multimegaitschool), [Instagram](https://instagram.com/kaperskyguru).

_If you are interested in backend development (or you’re internet enthusiast) both (Mobile | Web | Desktop) videos subscribe to my_ [_Youtube channel_](http://bit.ly/multimegaschool)_, 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](http://bit.ly/multimegaschool).

Sharing is caring.

* * *