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

Create HTTPX Custom Auth to handle token refresh #73

Open
1 task
ericcccsliu opened this issue Apr 11, 2023 · 0 comments · Fixed by #81 · May be fixed by #87
Open
1 task

Create HTTPX Custom Auth to handle token refresh #73

ericcccsliu opened this issue Apr 11, 2023 · 0 comments · Fixed by #81 · May be fixed by #87
Assignees

Comments

@ericcccsliu
Copy link
Collaborator

After creating a function to get the currently cached Enode access token and another to refresh it if necessary, we'll want to create a custom HTTPx Auth class to handle the logic for actually determining whether our current token needs to be refreshed. The way we do this is by, after someone makes a request to Enode's API, checking the response HTTP status code. If it's a 401, then we know our access token is no longer valid, and we must request another.

The way this is implemented in HTTPx is by creating a custom Auth class, which we'll pass into our HTTPx clients auth argument. See the example under this paragraph in the docs.

  • Create the custom Auth class to handle automatically determining token expiration and refreshing it, then re-requesting the resource

Here's how this code looks in TypeScript in our own mock API:

https://github.com/openclimatefix/pv-sites-mobile/blob/c5f47fde4c6c2cce6f145f58edbeddbca687be11/lib/enode.ts#L85-L108

@AndrewLester AndrewLester linked a pull request Apr 24, 2023 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants