Refreshing Oauth2 Tokens In Async Js
I have a single page app that talks to an API. I own both parts (the API and the single page app aka 'official client'). The client is javascript, so asynchronous. The problem I ha
Solution 1:
Found out myself, this works for me (CoffeeScript, Object.clone is from Sugar.JS):
tokenReloadPromise = null
$.ajaxPrefilter (options, userOptions, xhr)=>
if tokenReloadPromise?
xhr.abort()
tokenReloadPromise.then ->
options.noTokenRefresh = true
$.ajax(options)
else
originalOptions = Object.clone(options)
# configure access token for request here
unless options.noTokenRefresh == true
options.error = (xhr)->
if xhr.status == 401
tokenReloadPromise ?= new jQuery.Deferred
tokenReloadPromise.then ->
originalOptions.noTokenRefresh = true
$.ajax(originalOptions)
App.execute "refresh:access_token", ->
promise = tokenReloadPromise
tokenReloadPromise = null
promise.resolve()
else
originalOptions.error.apply(this, arguments)
true
Post a Comment for "Refreshing Oauth2 Tokens In Async Js"