ItSolutionStuff.com

How to Set Custom Redirect URL After Login in Laravel Jetstream?

By Hardik Savani • November 13, 2024
Laravel

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

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Jetstream Auth: Login With Email Or Username Example

Read Now →

Laravel Breeze Auth: Login With Email Or Username Example

Read Now →

How to Customize Laravel Breeze Authentication?

Read Now →

Laravel Breeze Change Redirect After Login/Register

Read Now →

Laravel 11 Breeze Multi Auth Tutorial

Read Now →

Laravel 11 Livewire CRUD using Jetstream & Tailwind CSS

Read Now →

Laravel 11 Authentication using Jetstream Tutorial

Read Now →

Laravel 11 Authentication - Install Laravel 11 Breeze Tutorial

Read Now →

How to Delete All Records Older Than 30 Days in Laravel?

Read Now →

How to Check Running Laravel App Environment?

Read Now →

Laravel Pluck Method Example | Laravel Pluck()

Read Now →