How do you know its not the second 'Unauthorised' ??? Why not change one of them to some other string
api Login not giving expected feedback message
In my Project I am using Laravel-8, laravel-passport and spatie-permission for restful api. Already, I have this code in the Controller.
I have this controller
public function adminLogin(Request $request)
{
if (auth('web')->attempt($request->only('email', 'password'))) {
$user = Auth::guard('web')->user();
if (!$user->hasRole('Super Admin')) {
auth('web')->logout();
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']);
}
}:
If the logged in user is not 'Super Admin' Role, it should return:
return $this->sendError('Unauthorised.', ['error'=>'Unauthorised']);
The logged in user is 'Super Admin', yet it returns the error.
'Super Admin' is part of the current user's role, yet I am getting this feedback:
{"success":false,"message":"Unauthorised.","data":{"error":"Unauthorised"}}
It has to do with this line:
if (!$user->hasRole('Super Admin')) {
auth('web')->logout();
return $this->sendError('Unauthorised.', ['error'=>'Unauthorised']);
}
database dump
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);
What could be the problem and how do I get it resolved?
Thanks
Please or to participate in this conversation.