Laravel 8 conditional login or signup redirects

Muhammad Abad
2 min readJan 27, 2021

--

Laravel 8 introduces Fortify, a new back-end package for providing user authentication. This represents a big departure from the controller with traits approach used in previous versions and has caused some concern that the authentication process is no longer customizable.

I used fortify in a project and want to redirect the user to a custom route, not on home as default. So unfortunately there is no option in Fortify documentation to do it. But we can do it by hooks that we require are bound into the container during the booting of the Laravel\Fortify\FortifyServiceProvider.

Within our own code, we can re-bind a different class where we will place our business logic.

Create our own Login Response Class

  1. Create a folder under app\Http called Responses
  2. Create a file LoginResponse.php

Simply, we need to change only in the intended() and we can put here our own route as I did in the below code.

This new class now replaces the Singleton previously registered by Fortify.

Edit the JetstreamServiceProvider orFortifyServiceProvider in your app\Providers folder;

In the boot method, add a reference to your new response class. When login completes (and the user is actually Authenticated) then your new response will be called.

Need to add below code of snippet in the boot method of app\Providers\FortifyServiceProvider.php file.

Same we can do to manage after registereration redirection.

Before you go… Thanks for reading the article! If you enjoyed it, please don’t forget to show your appreciation by clicking 👏 below!

I write about my professional experiences as a self-taught software developer, so click the follow button if this interests you!

--

--

Muhammad Abad

I’m Abad based from the India & I am working as a Software Developer. I specialize in building applications specific to the business needs of my clients.