I'm trying to setup a simple iOS example to better understand Siesta. My REST api requires an access token to accompany each request. So (1) at the start of the app and (2) anytime I retrieve a HTTP 401 I need to request an access token and then put that in all future Authorization headers.
Working off this example from the documentation, I assume the line containing showLoginScreen
is where I need to make a call to my authenticationResource
to retrieve the token BUT how do I make the failed call immediately after (and not infinite loop of course)? Thank you.
let authURL = authenticationResource.url
configure({ url in url != authURL }, description: "catch auth failures") {
$0.config.beforeStartingRequest { _, req in // For all resources except auth:
req.onFailure { error in // If a request fails...
if error.httpStatusCode == 401 { // ...with a 401...
showLoginScreen() // ...then prompt the user to log in
}
}
}
}
Since you asked the question, the docs have been updated with an example that answers it.
The crux of it is to use
decorateRequests(…)
andRequest.chained(…)
to wrap all your service’s requests so that they automatically attempt to refresh the token before returning a response.Here is the code from that example: