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

luke_wilson180's avatar

Login redirects in Jetstream

Hey, Iv been trying to redirect user to the correct dashboard after login either admin, teacher or student.

So far I am trying to just get admin working.

I have followed these guys and been working on this issue for two days now. laravel-news.com/override-login-redirects-in-jetstream-fortify

FortifyServiceProvider.php

    public function boot()
    {
        $this->app->singleton(LoginResponseContract::class, LoginResponse::class);

		Fortify::createUsersUsing(CreateNewUser::class);
        Fortify::updateUserProfileInformationUsing(UpdateUserProfileInformation::class);
        Fortify::updateUserPasswordsUsing(UpdateUserPassword::class);
        Fortify::resetUserPasswordsUsing(ResetUserPassword::class);
    }

LoginResponse.php

public function toResponse($request) 
{
        $home = auth()->user()->role == 'admin' ? '/admin/dashboard/index' : '/dashboard';
        return redirect()->intended($home);
    }

When I then login it says page not found, the view does exist in the correct directory. Do I have to register it in the Web.php Route? Also how would I go about adding the redirect for teacher and student as well?

Im new and this is for a university project so any help would be much appreciated. Thanks :)

0 likes
7 replies
luke_wilson180's avatar

@jlrdw Hey, His answer was originally the first one and I have seen his thread of answers in a few other questions too. However when I try his method I still get the same error I am currently receiving:

Call to undefined method App\Http\Responses\LoginResponse::show()

Would you know to solve this?

jlrdw's avatar

@luke_wilson180 don't do the actual redirect inside the ternary. See my example in that post of how I redirected, the one where I tested his method. It works.

luke_wilson180's avatar

@jlrdw I tried that now im getting a 404 | Not Found.

Do I have to declare it in the web.php Routes?

<?php

namespace App\Http\Responses;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;

class LoginResponse implements LoginResponseContract
{
    /**
     * @param  $request
     * @return mixed
     */
    public function toResponse($request)
    {
        $role = Auth::user()->role;
        $checkrole = explode(',', $role);  // However you get role

        if (in_array('admin', $checkrole)) {
            Session::put('isadmin', 'admin');
            return redirect('/admin/dashboard/index');
        } else {
            return redirect('/student/dashboard/index');
        }

        return $request->wantsJson()
            ? response()->json(['two_factor' => false])
            : redirect()->intended(config('fortify.home'));
    }

}

Sorry about all the questions, just really need to get this working :)

jlrdw's avatar

@luke_wilson180 change this:

        $role = Auth::user()->role;
        $checkrole = explode(',', $role);  // However you get role


To how you verify a role then redirect if the role matches. I don't know how you store roles. But the redirects should work.

You may have a roles table, that I don't know.

Edit:

And it was example that I use, don't do the Session::put('isadmin', 'admin'); that is something I use.

If you are new to laravel, I suggest taking the laravel 8 from scratch free training from @jeffreyway

luke_wilson180's avatar
luke_wilson180
OP
Best Answer
Level 1

@jlrdw

Thanks for the help. I edited the following so the user gets directed correctly based on role.

public function toResponse($request)
    {
        $role = Auth::user()->role;

        if ($role == 'admin') {
            return redirect('admin/dashboard');
        } else if ($role == 'teacher'){
            return redirect('teacher/dashboard');
        } else if ($role == 'student'){
            return redirect('student/dashboard');
    }

        return $request->wantsJson()
            ? response()->json(['two_factor' => false])
            : redirect()->intended(config('fortify.home'));
    }

For anyone in the future I got it working by adding the routes within the web.php. This is the only way I was able to get it working.

Route::middleware(['auth:sanctum', 'verified'])->get('admin/dashboard', function () {
    return view('admin/dashboard/index');
})->name('admin.dashboard.index');

Route::middleware(['auth:sanctum', 'verified'])->get('teacher/dashboard', function () {
    return view('teacher/dashboard/index');
})->name('teacher.dashboard.index');

Route::middleware(['auth:sanctum', 'verified'])->get('student/dashboard', function () {
    return view('student/dashboard/index');
})->name('student.dashboard.index');

Thanks

jlrdw's avatar

So glad you figured it out. And yes you will need Auth routes, since authentication works with a logged in user.

That is the whole point, redirecting depending on user role.

Please or to participate in this conversation.