React-native-app-auth: Refreshing a token outside of a UI action handler

Created on 26 Apr 2018  Â·  5Comments  Â·  Source: FormidableLabs/react-native-app-auth

I'm running into an issue trying to make the refreshing of a token seamless to the user, and looking for a recommendation on how to utilize refresh. Similar to the example application, I've gotten the refresh function to work just fine if it is triggered in an async function with an await for the call into refresh.

What I'm trying to do here is read the last received authorization response from 'authorize' from storage, and if the token is expired, attempt to refresh it. I'm using redux and redux-saga, so my plan was to have a 'refreshNeeded' indicator added to the store, causing the current view to update (which it does), and in 'componentDidUpdate' attempt the refresh. For example:

componentDidUpdate() {
    if (this.props.refreshNeeded) {
      console.log('WecomeScreen: componentDidMount: refreshNeeded');
      refresh(AppConstants.AUTH, { refreshToken: this.props.refreshToken }).then((response) => {
        console.log('WecomeScreen: componentDidMount: refreshDone, new token is ', response);
      }).catch((e) => {
        console.log('WecomeScreen: componentDidMount: errored', e);
      })
    }
  }

At first I had all this being done inside a saga, and I thought the error was just due to that. So I moved it out into this view, but I'm getting the same error:

WecomeScreen: componentDidMount: errored Error: Failed refresh token

e:Error: Failed refresh token at createErrorFromErrorData (blob:http://localhost:8081/81186e3c-7743-4808-8f0a-9b4117863aac:2039:17) at blob:http://localhost:8081/81186e3c-7743-4808-8f0a-9b4117863aac:1991:27 at MessageQueue.__invokeCallback (blob:http://localhost:8081/81186e3c-7743-4808-8f0a-9b4117863aac:2433:18) at blob:http://localhost:8081/81186e3c-7743-4808-8f0a-9b4117863aac:2178:18 at MessageQueue.__guardSafe (blob:http://localhost:8081/81186e3c-7743-4808-8f0a-9b4117863aac:2346:11) at MessageQueue.invokeCallbackAndReturnFlushedQueue (blob:http://localhost:8081/81186e3c-7743-4808-8f0a-9b4117863aac:2177:14) at http://localhost:8081/debugger-ui/debuggerWorker.js:70:58

**code:"RNAppAuth Error"**
framesToPop:1
message:"Failed refresh token"
stack:"Error: Failed refresh token↵    at createErrorFromErrorData (blob:http://localhost:8081/81186e3c-7743-4808-8f0a-9b4117863aac:2039:17)↵    at blob:http://localhost:8081/81186e3c-7743-4808-8f0a-9b4117863aac:1991:27↵    at MessageQueue.__invokeCallback (blob:http://localhost:8081/81186e3c-7743-4808-8f0a-9b4117863aac:2433:18)↵    at blob:http://localhost:8081/81186e3c-7743-4808-8f0a-9b4117863aac:2178:18↵    at MessageQueue.__guardSafe (blob:http://localhost:8081/81186e3c-7743-4808-8f0a-9b4117863aac:2346:11)↵    at MessageQueue.invokeCallbackAndReturnFlushedQueue (blob:http://localhost:8081/81186e3c-7743-4808-8f0a-9b4117863aac:2177:14)↵    at http://localhost:8081/debugger-ui/debuggerWorker.js:70:58"
__proto__

Is what I'm trying to do just not possible? Any suggestions on getting 'refresh' to work in a way that won't involve a user clicking something? I just upgraded to 2.4.1 from 2.4.0 hoping it was that issue with the secret not being seen, but no dice.

Most helpful comment

Sigh.. my mistake. It looks like it was attempting to refresh a stale token. I've confirmed everything works fine, even from within a saga when refreshing a fresh token.

All 5 comments

It looks like the refresh request itself is failing. Which auth provider are you using for your backend? Have you ever managed to get the refresh method to work?

This is a custom provider (not the usual Facebook / OpenID etc). I have gotten both authorize and refresh to work successfully in my own application against this provider when called from an onPress method. But as soon as I moved that same refresh call out of an async function called by an onPress handling method into componentDidUpdate (or into a redux saga), I'm seeing the refresh failing.

I'll double check that it's sending down the same auth structure as it was before when it was working, and move it back into a press handler to see if it starts working again

Edit: Huh.. I just ran the same code on a different computer with the same Android device and I saw it work once from a componentDidUpdate. Then... it started throwing the same exception again. I'll experiment a bit more to see if I can narrow it down.

Maybe try running the Android app in debug mode and put a breakpoint to where the error is thrown to see if there is any additional detail? I think it's most likely here. The token response will be null, but you could inspect to see if the AuthorizationException has more detail.

Will do, thanks

Sigh.. my mistake. It looks like it was attempting to refresh a stale token. I've confirmed everything works fine, even from within a saga when refreshing a fresh token.

Was this page helpful?
0 / 5 - 0 ratings