How to Set Custom Redirect URL After Login in Laravel Jetstream?
In this post, I will show how to change redirect url after login in laravel jetstream.
By default, Laravel Jetstream redirects users to the dashboard after they log in. If you want to change the redirect URL after login, follow these steps. In this example, we will change the URL from "/dashboard" to "/home".
Let's follow the following steps, After installed successfully laravel jetstream:
Step 1: Create LoginResponse Class
In this step, create a "Responses" folder and then add a file named LoginResponse.php. After that, update the following code. I have set the "home" URL after login, but you can change it if needed.
app/Http/Responses/LoginResponse.php
<?php
namespace App\Http\Responses;
use Illuminate\Support\Facades\Auth;
use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;
class LoginResponse implements LoginResponseContract
{
public function toResponse($request)
{
// below is the existing response
// replace this with your own code
// the user can be located with Auth facade
return $request->wantsJson()
? response()->json(['two_factor' => false])
: redirect()->intended("/home");
}
}
Step 2: Update JetstreamServiceProvider
In this step, we need to update boot() method inside the JetstreamServiceProvider file. so, let's update it.
app/Providers/JetstreamServiceProvider.php
<?php
namespace App\Providers;
use App\Actions\Jetstream\DeleteUser;
use Illuminate\Support\ServiceProvider;
use Laravel\Jetstream\Jetstream;
class JetstreamServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
//
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
$this->configurePermissions();
Jetstream::deleteUsersUsing(DeleteUser::class);
$this->app->singleton(
\Laravel\Fortify\Contracts\LoginResponse::class,
\App\Http\Responses\LoginResponse::class
);
}
/**
* Configure the permissions that are available within the application.
*/
protected function configurePermissions(): void
{
Jetstream::defaultApiTokenPermissions(['read']);
Jetstream::permissions([
'create',
'read',
'update',
'delete',
]);
}
}
Step 3: Autoload Class
In this step, we simple autoload all the class. so, run the following command:
composer dump-autoload
Now, you can run the application and login user. it will redirect "/home" page after login.
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Jetstream Auth: Login With Email Or Username Example
- Laravel Breeze Auth: Login With Email Or Username Example
- How to Customize Laravel Breeze Authentication?
- Laravel Breeze Change Redirect After Login/Register
- Laravel 11 Breeze Multi Auth Tutorial
- Laravel 11 Livewire CRUD using Jetstream & Tailwind CSS
- Laravel 11 Authentication using Jetstream Tutorial
- Laravel 11 Authentication - Install Laravel 11 Breeze Tutorial
- How to Delete All Records Older Than 30 Days in Laravel?
- How to Check Running Laravel App Environment?
- Laravel Pluck Method Example | Laravel Pluck()