Cannot redirect user based on role #5709
-
Description:I am attempting to redirect a user based on role (I use Spatie) on their first login however no matter what I try nothing seems to work - I have tried register() function in Nova Service Provider as suggested in docs however this is not redirecting me: public function register()
{
$user = request()->user();
if ($user) {
if ($user->hasRole('super-admin')) {
dump("You are super!");
Nova::initialPath('/resources/campaigns');
} elseif ($user->hasRole('importer')) {
dump("Come in importer!");
Nova::initialPath('/csv-import');
} else {
// default path for other roles
Nova::initialPath('/resources/users');
}
}
} The dump is coming through but the user is still going to dashboard by default. Thank you! Detailed steps to reproduce the issue on a fresh Nova installation: |
Beta Was this translation helpful? Give feedback.
Answered by
crynobone
Jul 11, 2023
Replies: 2 comments 4 replies
-
You can do the following: use Laravel\Nova\Events\ServingNova;
use Laravel\Nova\Nova;
Nova::serving(function (ServingNova $event) {
/** @var \App\Models\User|null $user */
$user = $event->request->user();
if (is_null($user)) {
return;
}
if ($user->hasRole('super-admin')) {
Nova::initialPath('/resources/campaigns');
} elseif ($user->hasRole('importer')) {
Nova::initialPath('/csv-import');
} else {
// default path for other roles
Nova::initialPath('/resources/users');
}
}); |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
HeadStudios
-
This worked for me in Event::listen(
\Illuminate\Auth\Events\Authenticated::class,
function () {
$user = Auth::user();
if (is_null($user)) {
return;
}
if ($user->hasRole(['management', 'management-admin'])) {
Nova::initialPath('/resources/users');
}
}
); |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can do the following: