Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

artisticre's avatar

Laravel 8 Fortify Redirect based on roles without jetstream

I am looking for a good tutorial or advice on how to redirect after authenticated based on roles in laravel 8 fortify without jetstream. I have found one that says to create a new LoginResonse.php in App\Http\Responses\loginresponse.php which I did but I am not sure where to register this new response because the tutorial I am using says to do it in Jetstreamserviceprovider but I am not using Jetstream. Any ideas?

0 likes
8 replies
artisticre's avatar

So I did that, thank you. But it still logs in as /home. This is what I am trying to do:

AppServiceProvider

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Schema::defaultStringLength(191);
        $this->app->singleton(
            \App\Http\Contracts\Contracts\LoginResponse::class,
            \App\Http\Responses\LoginResponse::class
        );
      
    }
}

LoginResonse.php



<?php

namespace App\Http\Responses;

use App\Http\Contracts\LoginResponse as LoginResponseContract;

class LoginResponse implements LoginResponseContract
{
    /**
     * Create an HTTP response that represents the object.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function toResponse($request)
    {
        // here i am checking if the currently logout in users has a role_id of 2 which make him a regular user and then redirect to the users dashboard else the admin dashboard
        if (auth()->user()->hasRole('admin')) {
            return redirect()->intended(config('admin.dashboard'));
        }

        if (auth()->user()->hasRole('blog')) {
            return redirect()->intended(config('blog.dashboard'));
        }

        if (auth()->user()->hasRole('editor')) {
            return redirect()->intended(config('editor.dashboard'));
        }
        
    }
}
artisticre's avatar

It seems like Laravel isn't recognizing my new LoginResponse.php. I added it to the AppServiceProvider but it still is logging in to /home regardless of what I have in the LoginResponse. Is there somewhere else I need to add it to?

MichalOravec's avatar

Don't use intended. Just classic redirect to route or path.

artisticre's avatar

OK I changed the LoginResponse and took out intended. Here it is. Still redirects only to /home, If I delete the vendor copy of LoginResponse, it gives an error so it tells me somewhere it is not recognizing my new version of LoginResponse but I am not sure where to make the change.



<?php

namespace App\Http\Responses;

use App\Http\Contracts\LoginResponse as LoginResponseContract;

class LoginResponse implements LoginResponseContract
{
    /**
     * Create an HTTP response that represents the object.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function toResponse($request)
    {
        if (auth()->user()->hasRole('admin')) {
            return redirect()->route('admin.dashboard');
    
        if (auth()->user()->hasRole('blog')) {
            return redirect()->route('blog.dashboard');
        }

        if (auth()->user()->hasRole('editor')) {
            return redirect()->route('editor.dashboard');
        }
        
    }
}

MichalOravec's avatar
Level 75

In the provider it has to be

$this->app->singleton(
    \Laravel\Fortify\Contracts\LoginResponse::class,
    \App\Http\Responses\LoginResponse::class
);

and not

$this->app->singleton(
    \App\Http\Contracts\Contracts\LoginResponse::class, // this is wrong
    \App\Http\Responses\LoginResponse::class
);
2 likes
artisticre's avatar

OK I added all that Now I get and I do h

Route [registrar.dashboard] not defined.

Route::get('/dashboard', [RegistrarController::class, 'getDashboard'])->name('registrar.dashboard')->middleware('auth','verified');

Registrar Controller

<?php

namespace App\Http\Controllers\Registrar;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class RegistrarController extends Controller
{
    public function getDashboard()
    {
        return view('registrar.dashboard');
    }
}

Please or to participate in this conversation.