I have an api rest made in laravel 5.6 where I authenticate and register with passport and oauth, I register successfully but I am having problems logging in with users that do not have administrator role, it tells me they are not authorized.
On the front I have no problems, I verify and if the login, email and password data are sent, they even pass the validation, the problem is that in my login function:
public function login(Request $request)
{
$credentials = [
'email' => $request->email,
'password' => $request->password
];
if (auth()->attempt($credentials)) {
$token = auth()->user()->createToken('TutsForWeb')->accessToken;
return response()->json(['token' => $token], 200);
} else {
return response()->json(['error' => 'UnAuthorised'], 401);
}
}
When the user has a role other than admin, the auth () -> attempt ($ credentials) statement becomes null and that is why I get error 401, I do not understand why, when logging in with a user with administrator role, It works correctly.