2
votes

I created a basic laravel passport authentication and created callback and redirect to authorize user below is my code

Route::get('/redirect', function () {
    $query = http_build_query([
        'client_id' => 4,
        'redirect_uri' => 'http://localhost:8000/callback',
        'response_type' => 'code',
        'scope' => '',
    ]);

    return redirect('http://localhost:8000/oauth/authorize?'.$query);
});

Route::get('/callback', function (Request $request) {
    $http = new GuzzleHttp\Client;

    $response = $http->post('http://localhost:8000/oauth/token', [
        'form_params' => [
            'grant_type' => 'authorization_code',
            'client_id' => 4,
            'client_secret' => 'MUqUFrtAx3ix84zFTxwqQXA5PQDY7SWwVFW9tCNX',
            'redirect_uri' => 'http://localhost:8000/callback',
            'code' => $request->code,
        ],
    ]);

    return json_decode((string) $response->getBody(), true);
});

I followed the steps provided on https://laravel.com/docs/5.4/passport but when i click on authorize button the page keeps loading without any response and i have to restart artisan serve. What could be the possible issue? enter image description here

3

3 Answers

1
votes

The issue is when using php artisan serve, it uses a PHP server which is single-threaded.

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

You can do this solution:

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.

Or 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' => 'authorization_code',
        'client_id' => 4,
        'client_secret' => 'MUqUFrtAx3ix84zFTxwqQXA5PQDY7SWwVFW9tCNX',
        'redirect_uri' => 'http://localhost:8000/callback',
        'code' => $request->code,
    ],
]);

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

0
votes

In your /redirect route you are providing same localhost:8000 . change your redirect_uri and same mistake in callback route .

Change Your 'redirect_uri' => 'http://localhost:8000/callback', To 'redirect_uri' => 'http://localhost:8001/callback',

Note: Im running other project on port 8001 .you can change it with yours :)

0
votes

try to use XAMPP or another web server instead of just php artisan serve