Search code examples
laravellaravel-socialitesuperoffice-api

Use Laravel Socialite user returns missing authorization_code


I'm using Laravel Socialite to login with SuperOffice API. Have only just added the provider as a pr but testing it already. I'm using the provider superoffice locally and inside a package superoffice-api I'm creating. Have added both packages to composer.json in the Laravel app:

"repositories": [
     {
         "type": "path",
         "url": "./packages/superoffice-api"
     },
     {
        "type": "path",
        "url": "./packages/superoffice"
     }
]

Also added the superoffice Socialite provider in the superoffice-api composer.json in the same way.

The login process is working but the problem starts when trying to use the user for other API calls. What I mean with this is on the callback I can do the following:

public function superofficeCallback(Request $request): RedirectResponse
{
     $user = Socialite::driver('superoffice')->stateless()->user();

     return redirect()->route('dashboard.index')->with([
         'message' => 'Loggedin with SuperOffice as '.$user->name,
         'success' => true,
      ]);
}

This shows the $user->name as expected. Now when trying to call Socialite::driver('superoffice')->stateless()->user() in the superoffice-api package I get the following error message:

GuzzleHttp\Exception\ClientException: Client error: POST https://sod.superoffice.com/login/common/oauth/tokens resulted in a 400 Bad Request response: { "error": "invalid_request", "error_description": "missing authorization_code"}

It doesn't matter if called in a method or in the __construct() of a class.

So my question is how can I use a Socialite provider superoffice user in a package superoffice-api? This is needed to get the access_token. Can imagine that because Socialite is called in a package some sort of reference is missing.


Solution

  • The problem here is that the access_token and refresh_token need to be stored in some other way in the callback function. When stored for example in the database you're able to use the tokens anywhere.