Jetstream Login Conditional Redirect
How to provide different redirects at login when using Laravel Fortify and Jetstream
Last updated
Was this helpful?
How to provide different redirects at login when using Laravel Fortify and Jetstream
Last updated
Was this helpful?
Laravel 8 introduces , a new back-end package for providing user authentication services. 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 customisable.
Of course, the maturity of the framework, and previous experiences would be unlikely to produce a framework where you could not override default behaviour.
Suppose we need to redirect the user as they are logging in based on some user attribute. With Fortify, how might this be possible?
The 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 a folder under app\Http called Responses
Create a file LoginResponse.php
For Example
This new class now replaces the Singleton previously registered by Fortify.
Edit the JetstreamServiceProvider
in your app\Providers
folder;
In the boot method, add reference to your new response class. When login completes (and the user is actually Authenticated) then your new response will be called.
If you use 2FA with Jetstream, you will also need to catch the TwoFactorLoginResponse. Use the same approach;
You can return the same response, or create an additional response if you want different behaviour for users that login using 2FA.