See snapey answer https://laracasts.com/discuss/channels/laravel/version-8-redirects
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 :)
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
Please or to participate in this conversation.