SWAFWAN P P
SWAFWAN P P

Reputation: 1

Passport password grant response issue

Anyone know to solve this problem? if i do a post request on url http://127.0.0.1:8000/oauth/token using the parameters directly in postman, it does provides me access token and refresh token. But when i do it through code in login function it gives curl timeout error

cURL error 28: Operation timed out after 30005 milliseconds with 0 bytes received (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for http://127.0.0.1:8000/oauth/token

API endpoint is http://127.0.0.1:8000/api/login

public function login(LoginRequest $request): JsonResponse
    {
        if (Auth::attempt(['email' => $request->email, 'password' => $request->password])) {

            $response = Http::asForm()->post(env('APP_URL') . '/oauth/token', [
                'grant_type' => 'password',
                'client_id' => env('PASSPORT_PASSWORD_CLIENT_ID'),
                'client_secret' => env('PASSPORT_PASSWORD_SECRET'),
                'username' => $request->email,
                'password' => $request->password,
                'scope' => '',
            ]);

            $token = $response->json();

            return response()->json([
                'success' => true,
                'statusCode' => 200,
                'message' => 'User has been logged successfully.',
                'data' => $token,
            ], 200);
        } else {
            return response()->json([
                'success' => true,
                'statusCode' => 401,
                'message' => 'Unauthorized.',
                'errors' => 'Unauthorized',
            ], 401);
        }

    }

expecting,

{
    "token_type": "Bearer",
    "expires_in": 1295999,
    "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIyIiwianRpIjoiYjdlM2U5ODFjYTFlZTU3OTQ1YWM3YjY0NmJlYzRhZmRiMmMxNjAyYzQyYzcwNzQ3YmI0MTM5OTI5ZDA0YjBlYjc4ZWM4NzlhZWRhNDFlOTEiLCJpYXQiOjE3MzcwMTI0MTAuMDA3NzY3LCJuYmYiOjE3MzcwMTI0MTAuMDA3NzcsImV4cCI6MTczODMwODQwOS45OTQ4MjUsInN1YiI6IjEiLCJzY29wZXMiOltdfQ.ppCR29LO0lJcqpPd8yxKueWqp1VNb8MJAeVI9lDzdvK8AcyfUR3SICJwbqGMTFO4mbl47E5bR7Iey24rRDUn",
    "refresh_token": "3532ca18dfabe19407b43237922b23aab97541dc56ceac59e56d01064cf6e602c6e77dbaa58d271f4b71554e3ad312d9bce68d83f8ec0c17fb2b96b4ce5be63dd1b940de900317ece48b45e076f7"
}

Upvotes: -1

Views: 72

Answers (1)

Alexis Gatuingt
Alexis Gatuingt

Reputation: 547

This usually occurs when the Laravel application tries to call itself but fails to establish a connection. There are several ways to solve this problem:

  • try excluding /oauth/token from CSRF middleware
  • Use the config function instead of env
  • Change the url to localhost
  • Use trim to remove unnecessary spaces and/or slashes
  • Use url method
  • Check with http prefix
  • Try to call the route from an api client (postman, insomnia)
  • Test on tinker

Upvotes: -2

Related Questions