4

We're adapting a laravel website into a laravel SPA, and in the future, a Mobile App that uses the web app, so we need some sort of API authentication. We're using Laravel Passport for that.

I've been using Laravel 5.5 and following the guide on the Docs, but when I try using the new login with postman, the server freezes, the HTTP requests never processing.

After some debugging, I found out that it crashes when I use Guzzle to post into the /oauth/token route. But when I use Postman to access that route, I have no problem.

This is my code:

    public function login(Request $request){
    $http = new Client();

    var_dump(1);
    //die
    $response = $http->post('http://localhost:8000/oauth/token', [  //Con postman esta ruta funciona
            'form_params' => [
            'grant_type' => 'password',
            'client_id' => env('PASSWORD-CLIENT_ID',2),
            'client_secret' => env('PASSWORD-CLIENT_SECRET',2),
            'username' => $request->username, //parece usar correo, no nombre de usuario
            'password' => $request->password,
            'scope' => '*',
        ],
    ]);

    var_dump(2);
    //die;
    return json_decode((string) $response->getBody(), true);
}

Is this a problem with Guzzle, Or with Oauth/Passport?

Eby Jacob
  • 1,418
  • 1
  • 10
  • 28

3 Answers3

4

If you are looking for a quick fix to test your updates - you can get this done by opening up two command prompts. The first would be running php artisan serve (locally my default port is 8000 and you would be running your site on http://localhost:8000). The second would run php artisan serve --port 8001.

Then you would update your post request to:

$response = $http->post('http://localhost:8001/oauth/token', [ 
        'form_params' => [
        'grant_type' => 'password',
        'client_id' => env('PASSWORD-CLIENT_ID',2),
        'client_secret' => env('PASSWORD-CLIENT_SECRET',2),
        'username' => $request->username,
        'password' => $request->password,
        'scope' => '*',
    ],
]);

This should help during your testing until you are able to everything on server or a local virtual host.

busytraining
  • 723
  • 8
  • 14
3

I encountered the same issue, in my case it wasn't Guzzle specifically that froze, I tried curl as well. The problem lied in the fact that I was running it all on 1 instance of the built-in PHP webserver which is single-threaded.

The web server runs only one single-threaded process, so PHP applications will stall if a request is blocked.

Source: http://php.net/manual/en/features.commandline.webserver.php

When making calls to itself the thread blocked waiting for its own reply. The solution is to either seperate the providing application and consuming application into their own instance or to run it on a multi-threaded webserver such as Apache or nginx.

Robin De Schepper
  • 4,942
  • 4
  • 35
  • 56
0

same thing happened to me if i serve the application thru CLI. What I did was I changed my development to homestead or xampp virtualhost and it worked.