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