id() ?? 0); if ($userId <= 0) { return $this->error('Please log in first.', Response::HTTP_UNAUTHORIZED); } $roles = $this->switchService->getUserRoleNames($userId); if (empty($roles)) { return $this->error('No roles assigned to this user.', Response::HTTP_NOT_FOUND); } return $this->success([ 'roles' => $roles, ]); } public function switch(RoleSwitchRequest $request): JsonResponse { $userId = (int) (auth()->id() ?? 0); if ($userId <= 0) { return $this->error('Please log in first.', Response::HTTP_UNAUTHORIZED); } $role = (string) $request->validated()['role']; try { $route = $this->switchService->switchRole($userId, $role); } catch (AccessDeniedHttpException $e) { return $this->error($e->getMessage(), Response::HTTP_FORBIDDEN); } catch (NotFoundHttpException $e) { return $this->error($e->getMessage(), Response::HTTP_NOT_FOUND); } return $this->success([ 'role' => $role, 'dashboard_route' => $route, ], 'Role switched successfully.'); } }