laravel / socialite

Laravel wrapper around OAuth 1 & OAuth 2 libraries.

Home Page:https://laravel.com/docs/socialite

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Fix] `getCode` returns `null` when try to send request from unit testing

heshamabdallah opened this issue · comments

  • Socialite Version: ^5.5
  • Laravel Version: ^9.19
  • PHP Version: ^8.0.2

Description:

The getCode inside Laravel\Socialite\Two\AbstractProvider returns null when request is sent from unit testing

$this->postJson("api/v1/login/social/{$provider}", [
    'code' => $code,
])
->assertSuccessful();

It works just fine when I send a request using CURL, Postman or from the browser.. I tried to dump the request inside the getCode function and I can see the sent request is GET instead of POST even when I dump the method inside the controller I get POST request method.

I can send post and postJson from unit testing just fine for other endpoints but it fails when I deal with Socialite.. it might be an issue with Laravel\Socialite\Two\AbstractProvider and how the request is passed to __construct

/**
 * Get the code from the request.
 *
 * @return string
 */
protected function getCode()
{
   dump([
      // `GET` when sent from unit testing and `POST` when sent from Postman
      'method' => $this->request->method(),
      
      // `null` when sent from unit testing and the correct value when sent from Postman
      'code' => $this->request->input('code'),
   ]);
   
    return $this->request->input('code');
}

Code sample

/**
 * Get social provider.
 *
 * @param \App\Http\Requests\User\AuthLoginSocialRequest  $request
 * @param string  $providerName
 * @return \Laravel\Socialite\Two\ProviderInterface
 */
protected function getSocialProvider(AuthLoginSocialRequest $request, string $providerName): ProviderInterface
{
    $provider = Socialite::driver($providerName)
        ->redirectUrl(
            $request->headers->get('origin') . '/login/social/' . $providerName
        )
        ->stateless();

    return $provider;
}

/**
 * Login user with code.
 *
 * @param \App\Http\Requests\User\AuthLoginSocialRequest  $request
 * @param string  $providerName
 * @return \Illuminate\Http\Response|array
 */
public function loginWithCode(AuthLoginSocialRequest $request, $providerName)
{
   dump([
      // `POST` in all cases, when sent from unit testing, CURL or from Postman
      'method' => $this->request->method(),
      
      // The correct value when sent from unit testing, CURL or from Postman
      'code' => $this->request->input('code'),
   ]);
    
    $user = $this->getSocialProvider($request, $providerName)
        ->user();

    //
}

Any idea what's wrong with it ?

Hi there,

Thanks for reporting but it looks like this is a question which can be asked on a support channel. Please only use this issue tracker for reporting bugs with the library itself. If you have a question on how to use functionality provided by this repo you can try one of the following channels:

However, this issue will not be locked and everyone is still free to discuss solutions to your problem!

Thanks.