Skip to content

Instantly share code, notes, and snippets.

@nderkach
Last active December 7, 2017 17:31
Show Gist options
  • Save nderkach/5e054375fc04e7b78a2705aaea734040 to your computer and use it in GitHub Desktop.
Save nderkach/5e054375fc04e7b78a2705aaea734040 to your computer and use it in GitHub Desktop.
func refreshAuth(_ username: String, _ password: String) -> Request {
return self.login(username, password, onSuccess: {
}, onFailure: { error in
})
}
func refreshTokenOnAuthFailure(request: Siesta.Request) -> Request {
return request.chained {
guard case .failure(let error) = $0.response, // Did request fail…
error.httpStatusCode == 401 else { // …because of expired token?
return .useThisResponse // If not, use the response we got.
}
return .passTo(
self.refreshAuth("test", "test").chained { // If so, first request a new token, then:
if case .failure = $0.response { // If token request failed…
return .useThisResponse // …report that error.
} else {
return .passTo(request.repeated()) // We have a new token! Repeat the original request.
}
}
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment