This is my UserController.php.I am using a laravel passport for API authentication
public function login(Request $request){
$request->validate([
'email'=>'required|email',
'password'=>'required',
]);
$credentials=$request->only(['email','password']);
if(Auth::attempt($credentials)){
$success['token']=Auth::user()->createToken('MyApp')->accessToken;
return response()->json([Auth::user(),'success'=>$success],200);
}else{
return response()->json(['error'=>'Not Found'],401);
}
}
This is my routes/api.php
Route::post('/user-login','UserController@login');
and i checked this URL in Postman and result come like this
http://127.0.0.1:8000/api/user-login
{
"user": {
"id": 1,
"name": "Admin User",
"email": "[email protected]",
"phone": "982356222",
"roles": [
{
"id": 1,
"name": "Sub-Admin",
"created_at": "2020-10-14 15:29:36",
"updated_at": "2020-10-14 15:29:36",
}
],
},
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxIiwianRpIjoiYjY3MTE3ZjMxZmEzN2RhMGIyZGEzMmM0NDEzMDA2MDFkMTVhM2Y0ODdhNzFmNWIyMDcxNGZmNWY4M2MzYTVlNTFiNjQyMmJjODlmMzExOGIiLCJpYXQiOjE2MDU3MDQ3NDgsIm5iZiI6MTYwNTcwNDc0OCwiZXhwIjoxNjM3MjQwNzQ4LCJzdWIiOiIxIiwic2NvcGVzIjpbXX0.KunRT2KA8hI7c2lJW7e0nvJnWirgBrNgFAAyQq-PG6znHLS14-gcP-2rXWOfuGreW6N2yBxfu6AOg2BYOHNOg54qmXNJ6_fJRrOUpAjgCxEN60X8x_7lE2Gu0xXYhw0Oc5jCeCgtLdeh9DlHFIh2Wdhnd9lU4FMLcQl3q0TZt0Yf7FpQtv2-q2izOQlxV9QWO5R6mzlmg5feaqTUpx4repb2JpaCMAF47ZBn6ZYPDNcJTQJ6OdU5bzc3GWF3V97qJFS9OabMvA8bUmY8opirIAPb2Lqd9zmP06YMHOP0oT_qr0duQIWnCPQp3bGSf9lUlF0tKil3NR8YZtOhkjr9W6uQoe5n5AsZLJrfdeRbgwPsANink8NdWzfvHkBhmCEKJyiwUFQhTQAgrwc_uflv6XbJsIzVzsxihjIdl4b9Y-hmA-PnXzb108aeKH0Ja5DlP55Zk6AB3kiWx-Zf39aSvi0lQJDN6u2YuNhRnXdceFIzyGM2iV0fGqRqPKeA0FyUo4wBD3cX5_HlLHk7FPyltqWkMvoozc-JP8lmdROJypoPCDo0tOQHiD9pbnRu4hJnhoomQr0YZ5t9Z7AXtErEHRatshST5McSin8tGmYdTG4hxOY2yUZl7NTyloUVb_1vpDqh214hXm8KtykhlD-N98fX3XWjfZIXDBeD7qOeGdM"
}
But I want to give access to which user has roles name as Sub-Admin.If user roles has name Administrator or anything then give message as Invalid Login Credentials