Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a bug in the demo README code? #46

Open
cam8001 opened this issue Aug 25, 2021 · 3 comments
Open

Is there a bug in the demo README code? #46

cam8001 opened this issue Aug 25, 2021 · 3 comments

Comments

@cam8001
Copy link

cam8001 commented Aug 25, 2021

There's some example code in the readme (very helpful). When you initially save a token as a result of the callback, there's this:

public function handleCallbackFromXero(Request $request) {
        // Lots of stuff omitted
        $accessToken = $this->getOAuth2()->getAccessTokenFromXeroRequest($request);
        $user->xero_access_token = json_encode($accessToken);
}

And then later on, when you save the refresh token:


    public function refreshAccessTokenIfNecessary()
    {
        // Step 5 - Before using the access token, check if it has expired and refresh it if necessary.
        $user = auth()->user();
        $accessToken = new AccessToken(json_decode($user->xero_access_token));

        if ($accessToken->hasExpired()) {
            $accessToken = $this->getOAuth2()->refreshAccessToken($accessToken);
            // Should this line use a json_encode() ?
            $user->xero_access_token = $accessToken;
            $user->save();
        }
    }

In the refreshAccessTokenIfNecessary method, should the access token be saved JSON encoded, or as a raw string like that? It is JSON encode when it is first saved.

@seanmccabe
Copy link

That is up to the user, some people prefer to save things as json, others as seperate strings.
Personally I save it as a json string, and then decode it out and use a getter to get the token into a string.

From memory though, I don't think you can use this code straight off the bat. Its not really a bug though, just a guide into using this and a little fill the gaps depending on your setup and use.

@cam8001
Copy link
Author

cam8001 commented Aug 29, 2021

I confused myself by using the code as-is but then realising I was sending JSON strings to Xero instead of the actual auth token. Would you be cool if I did a small PR to the readme demo code to describe the data formats?

@chrisloftus
Copy link

@seanmccabe How is this right when refreshAccessToken doesn't return the token expiry time? So then the hasExpired method won't know when the new token has expired?

Whereas getAccessTokenFromXeroRequest returns the token and expiry..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants