4

I'm using entrust package to manage roles and I have used passport for authentication since it is API's.

Actually what I need is want to check user has a role,

I have tried with below code but it doesn't work

public function Adminlogin()
{
    if(Auth::attempt(['email' => request('email'), 'password' => request('password')]))
    {
    $user = Auth::user();
    if($user->hasRole('admin'));
    {
        $success['token'] =  $user->createToken('MyApp')->accessToken;
        return response()->json(['success' => $success], $this->successStatus);
        }
        return response()->json(['error'=>'Abort'], 403);

    }
    else
    {
        return response()->json(['error'=>'Unauthorised'], 401);
    }
}

I want to generate access token only if user has admin role,If user has no role admin then show abort message.

Manjunarth
  • 313
  • 2
  • 7
  • 22
  • You have a semicolon at the end of the second if, that may relate to the problem you described in the first answer's comment. – Core-i9 Dec 30 '17 at 12:33

1 Answers1

7
if(Auth::user()->hasRole('admin'))

should work.

But you don't explain what is not working in your case so...