Laravel api登录给出错误的反馈消息
在我的项目中,我使用Laravel-8、Laravel passport和restful 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();
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(),我已转储数据库。它是为指定的用户准备的。谢谢