webfox / laravel-xero-oauth2

A Laravel integration for Xero using the Oauth 2.0 spec
MIT License
50 stars 32 forks source link

xero.json not creating after redirecting back from Xero login #84

Closed harisiddigital closed 8 months ago

harisiddigital commented 1 year ago

Hi I go to the route "manage/xero" and click on connect to xero. I enter my credentials and login to xero and get redirected back to my site on "/xero/auth/callback?code=XXXXXX"

Then i go and check storage directory, xero.json isn't created with credentials.

When I first tried this library it worked, xero.json was created, but that xero account expired now Im connecting with a new one. I deleted the xero.json file and went through the same process but this time file isn't created.

I have checked disks are all correct.

harisiddigital commented 1 year ago
public function index(Request $request, OauthCredentialManager $xeroCredentials)
    {
        try {
            // Check if we've got any stored credentials
            if ($xeroCredentials->exists()) {
                if($xeroCredentials->isExpired()) {
                    $xeroConfig = \XeroAPI\XeroPHP\Configuration::getDefaultConfiguration();
                    $xeroCredentials->refresh();
                    $xeroConfig->setAccessToken($xeroCredentials->getAccessToken());
                }
                /*
                 * We have stored credentials so we can resolve the AccountingApi,
                 * If we were sure we already had some stored credentials then we could just resolve this through the controller
                 * But since we use this route for the initial authentication we cannot be sure!
                 */
                $xero             = resolve(\XeroAPI\XeroPHP\Api\AccountingApi::class);
                $organisationName = $xero->getOrganisations($xeroCredentials->getTenantId())->getOrganisations()[0]->getName();
                $user             = $xeroCredentials->getUser();
                $username         = "{$user['given_name']} {$user['family_name']} ({$user['username']})";
            }
        } catch (\throwable $e) {
            // This can happen if the credentials have been revoked or there is an error with the organisation (e.g. it's expired)
            $error = $e->getMessage();
        }

        return view('xero', [
            'connected'        => $xeroCredentials->exists(),
            'error'            => $error ?? null,
            'organisationName' => $organisationName ?? null,
            'username'         => $username ?? null
        ]);
    }

    public function callback(Request $request) {

    }

this the code in my controller