Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel api登录给出错误的反馈消息_Laravel - Fatal编程技术网

Laravel api登录给出错误的反馈消息

Laravel api登录给出错误的反馈消息,laravel,Laravel,在我的项目中,我使用Laravel-8、Laravel passport和restful api的空间许可。我在控制器中已经有了这个代码 我有这个控制器 public function adminLogin(Request $request) { if (auth('web')->attempt($request->only('email', 'password'))) { $user = Auth::guard('web')->user();

在我的项目中,我使用Laravel-8、Laravel passport和restful api的空间许可。我在控制器中已经有了这个代码

我有这个控制器

public function adminLogin(Request $request)
{
    if (auth('web')->attempt($request->only('email', 'password'))) {
        $user = Auth::guard('web')->user();
      //  dd($user);
        if (!$user->hasRole('Super Admin')) {
            return $this->sendError('Unauthorised.', ['error'=>'Unauthorised']);
        }

        $success['token'] =  $user->createToken('MyApp')-> accessToken;
        $success['name'] =  $user->name;

        return $this->sendResponse($success, 'User login successfully.');
    }
    else{
        return $this->sendError('Unauthorised.', ['error'=>'Unauthorised']);
    }
}:
“超级管理员”是当前用户角色的一部分,但我收到了以下反馈:

{“成功”:false,“消息”:“未授权”。,“数据”:{“错误”:“未授权”}

这与这一行有关:

if (!$user->hasRole('Super Admin')) {
  return $this->sendError('Unauthorised.', ['error'=>'Unauthorised']);
}


CREATE TABLE `roles` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `guard_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;


INSERT INTO `roles` (`id`, `name`, `guard_name`, `created_at`, `updated_at`) 
VALUES
(1, 'Super Admin', 'web', '2019-11-13 12:11:38', NULL),
(2, 'Employee', 'web', '2019-11-13 12:11:38', NULL),
(3, 'HOD', 'web', '2019-11-13 12:11:38', NULL);


CREATE TABLE `model_has_roles` (
  `role_id` bigint(20) UNSIGNED NOT NULL,
  `model_type` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `model_id` bigint(20) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO `model_has_roles` (`role_id`, `model_type`, `model_id`) VALUES
(1, 'App\\Models\\User', 1),
(2, 'App\\Models\\User', 1);
问题可能是什么?我如何解决


谢谢

您确定经过身份验证的用户具有该角色吗,
auth()->user()->getRoleNames()的输出是什么?您应该获得分配给用户的所有角色的集合。@Unflux-Auth::guard('web')->user()->getRoleNames();当auth()->user()->getRoleNames()时,给出illumb\Support\Collection{1348#items:[]};给出:错误:调用null上的成员函数getRoleNames(),我已转储数据库。它是为指定的用户准备的。谢谢