andy0517
4 months ago
761
2
Laravel

Laravel passport API, method attempt does not exist.

Posted 4 months ago by andy0517

Currently I am using the passport to do the API authentication. While I doing the login function, it returns me BadMethodCallException method attempt does not exist.

public function login(Request $request)
    {

      $validator = Validator::make($request->all(), [
          'email' => 'required|email',
          'password' => 'required',
        ]);
      if ($validator->fails()) {
        return response()->json(['error'=>$validator->errors()], 401);
      }

      $credentials = $request->only('email', 'password');

      if(Auth::guard('member-api')->attempt($credentials))
      {
        //return response()->json(['sucess'=>'Success'], 401);
        $user = Auth::guard('member-api')->user();
        $success['token'] =  $user->createToken('asApp')->accessToken;
        return response()->json(['success' => $success], 200);
      }else {
        return response()->json(['error'=>'Email or password incorrect'], 401);
      }
'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'passport',
            'provider' => 'users',
        ],
        'member-api' => [
          'driver' => 'passport',
          'provider' => 'members',
        ],
    ],

it seems like the guard in front of attempt cause the problem, but i can't find the solution to solve it. Anyone have idea about it?

Please sign in or create an account to participate in this conversation.