Skip to content
Advertisement

How to create a password reset method in Laravel when using the Database User Provider

I’ve been searching the internet and have yet to find a solution to the following problem…

We currently have a website developed using Laravel which the user table is a remote Microsoft SQL database. The driver in config/auth.php has been set to “database”. All is working fine except for the password reset functionality, which we get the following error:

JavaScript

From my limited understanding of Laravel (this is my first experiance with Laravel), the Eloquent driver has support for the CanResetPassword functionality, however, this has not been implemented in the Database User Provider by Laravel, hence the error.

So my question is thus, has anyone had a configuration where they have the driver to “Database” and implemented a reset password functionality? All the examples I have seen to date relate to using the Eloquent model, which from my understanding of Laravel is not an option since during the initial development we had to change the driver from Eloquent to database to get the remote Microsoft SQL server working in the first place. Moving the Microsoft SQL database to a local database is not an option I’m afraid.

Alternatively, if anyone has implemented another method of a user resetting their password using an email address I would be open to suggestions.

Advertisement

Answer

To write your own password reset logic, you can still use the default migration that comes out of the box or simply create yours. The most important part is the token. Because you are making your own password reset, you have a couple of decisions to make:

  • Will the token expire?
  • Can a user use the same token multiple times?

You will need 2 pages, 4 different routes and 4 different functions in the same controller. The ‘I forgot my password’ page and the ‘Reset password’ page. In the first page, display a form where you take the user email. And post to the following controller.

JavaScript

Second part, when the user clicks on the link

JavaScript

Display a page with a form containing 2 inputs – New password password or whateveer you want – New password confirmation password_confirm or whatever you want The form should post to the same URL mapped to the following controller. Why? because we still need to use the token to find the actual user.

JavaScript

Don’t forget to add routes

JavaScript

Note: There might be typos or syntax errors because I did not test this and wrote it here directly from the top of my head. If you see an error/exception, don’t panick, read the error and search google.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement