I’ve not done it myself, but it looks like you would need to create a Http.Resolver to check the status code of the Http.Response from your first request, and another Http.Resolver for the refresh token request. The trick would be to write these resolvers in such a way that they “pass along” the details of the initial request, so that you can then reissue it if you need to.
You can then use Http.task to turn the requests in to tasks and Task.onError to conditionally run the refresh request (which itself should be chained with Task.andThen to rerun the initial request on success). I guess you’d also need some way to avoid infinite loops if for some reason the refresh succeeds but the initial request still fails, so you would maybe also want to pass some kind of retry count along through the custom resolvers.
I’m afraid I don’t have time right now to try this myself, so I may have missed some details. I should point out that if you search the package database there are also some existing OAuth packages for elm out there…
I have a similar situation against the Drupal REST API. But so far have been unable to come up with something different then handling the error case in my update function, and retrying there.
I received some suggestions here, have a look, as they are similar to your proposal. Haven’t had the time to try them yet.
I am currently using Timer method to refresh the token. However, when the computer goes sleep or inactive, the setTimeout feature of browser doesn’t execute the refresh on time.
I curious about the proper way to handle refresh token workflow in elm.
I think the short answer is that in case you want to persist new state like new token you need to do so via update function as there is no other way to update the model in tea.
The second question is how would you go about it. And answer to that is that there are different ways with different tradeoffs. I would personally suggest use something you’re familiar with. It can be anything on a scale from the most simple function String -> Model -> Model to state monad. Passing functions in Msgs or avoiding that.