Skip to content
Advertisement

Laravel Passport callback route returns null

I am learning Laravel Passport and developing an OAuth2 server. After creating a client I made the following call to get the authorization code

Route::get('/', function (Request $request) {
    $request->session()->put('state', $state = Str::random(40));

    $query = http_build_query([
        'client_id' => <Client_Id>,
        'redirect_uri' => 'http://<Consumer_App_URL>/callback',
        'response_type' => 'code',
        'scope' => '',
        'state' => $state,
    ]);

    return redirect('http://<OAuth2_Server_URL>/oauth/authorize?'.$query);
});

The call works as expected and I got the authorization code, however when redirecting to the callback route which is defined like so

Route::get('/callback', function (Request $request) {
    $state = $request->session()->pull('state');

    throw_unless(
        strlen($state) > 0 && $state === $request->state,
        InvalidArgumentException::class
    );

    $response = Http::asForm()->post('http://<OAuth2_Server_URL>/oauth/token', [
        'grant_type' => 'authorization_code',
        'client_id' => <Client_Id>,
        'client_secret' => '<Cient_Secret>',
        'redirect_uri' => 'http://consumer/callback',
        'code' => $request->code,
    ]);

    return $response->json();
});

It didn’t work, the response instance is just null. I can’t figure out what might be the issue.

Advertisement

Answer

So I finally solved the problem, I just have to run php artisan config:cache . [reference]

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement