truqu / elm-oauth2

OAuth 2.0 client-side utils in Elm
MIT License
81 stars 29 forks source link

Add `makeTokenRequest` version that doesn't require toMsg as a parameter and returns a Task instead #28

Open adrianbunea opened 3 years ago

adrianbunea commented 3 years ago

I tried two ways of doing a refresh flow in my application.

First one, using Task.andThen and Task.onError, I wanted to attempt a request, refresh if the request failed with 401 and retry the request afterwards with the new token, and this would ideally only require one update cycle because the task would do all these things in one go behind the scenes, but I couldn't because makeTokenRequest required a message, and didn't return a Task or something that I could turn into a Task, so I couldn't make a reusable request wrapper.

The second way was to receive the expiresIn, schedule a new refresh in that time, and calculate expiresAt using expiresIn and Time.now and store this info in Local Storage so I know if I have to refresh when opening the app. Ideally I would use Task.map2 to combine the result of the refresh request with the result of Time.now, and then store it, however, makeTokenRequest is not a Task so I cannot do this, so I have to handle the result of Time.now into a separate update cycle, which makes this less reusable as well.

In both cases, I felt that requiring toMsg and not returning a Task made this impossible to compose, which would be very useful. Maybe there is a way I don't know of yet, so I am open to ideas, but I would like to know your opinion about this request.