Redirect in fortify base on roles Hi, I've implemented roles as many to many, I have 5 roles . admin, student, teacher, accountant,dean. How can I redirect them to separate dashboard, after login? Currently it redirects everyone to the "home" uri.
I guess I need to change this
'home' => RouteServiceProvider::HOME,
in config/fortify.php
Will all the dashboard be under same route or same route group as the respective roles ?
@Snapey just to clarify do i call all my roles under the redirect? like this
redirect()->intended(
auth()->user()->is_superadmin ? route('superadmin.dashboard') : route('dashboard'),
auth()->user()->is_admin ? route('admin.dashboard') : route('dashboard'),
auth()->user()->is_Hr ? route('hr.dashboard') : route('dashboard'),
auth()->user()->is_clack ? route('clack.dashboard') : route('dashboard'),
auth()->user()->is_worker ? route('workes.dashboard') : route('dashboard'),
);
or do i use if elseif statement
@youngswagx no, thats not valid code
use a match or switch statement to return the right url depending upon the role
i deed something like this but if keep redirecting me back to default
public function toResponse($request) {
return $request->wantsJson() ?
response()->json(['two_factor' => false]) :
redirect()->intended($this->getIntendedRoute());
}
public function getIntendedRoute() {
return match(true) {
auth()->user()->is_admin => route('admin.dashboard'),
auth()->user()->is_Principal => route('principal.dashboard'),
auth()->user()->is_Teacher => route('teachers.dashboard'),
auth()->user()->is_Student => route('students.dashboard'),
auth()->user()->is_Parent => route('parents.dashboard'),
default => route('dashboard'),
};
}
am i doing it wrong ?
route
Route::prefix('admin')->middleware(['auth','auth.isAdmin'])->group(function () {
Route::get('/dashboard', [AdminDashboardController::class, 'dashboard'])->name('admin.dashboard');
});
Please sign in or create an account to participate in this conversation.