Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to update config after refresh token. #710

Closed
panda919 opened this issue May 5, 2019 · 24 comments
Closed

How to update config after refresh token. #710

panda919 opened this issue May 5, 2019 · 24 comments

Comments

@panda919
Copy link

panda919 commented May 5, 2019

Your Environment

  • Plugin version:
  • Platform: iOS or Android
  • OS version:
  • Device manufacturer / model:
  • React Native version (react-native -v): 0.55.4
  • Plugin config

Expected Behavior

Actual Behavior

Context

I used JWT troken auth.
When token is expired, i get new auth token and update BackgroundGeolocation 's config and call sync() function.
BackgroundGeolocation.setConfig({
headers: ------,//new auth token header
reset: true,
});
this.sync();
But, I got 401 error yet.
What's wrong?

Debug logs

@mikehardy
Copy link
Contributor

I'm also interested in this - @panda919 am I correct in saying that in the general sense the question is: If you already launched your app and have this plugin running, what is the best way to dynamically alter it's config?

My case is that I want extra user information available in the "extras" but I only have it available in certain situations and definitely only after app launch. If I'm able to get that info I'd like to alter the info included in extras and have that change take affect

I suppose I/we could do a stop/config/ready-with-reset ? (carefully checking state even)

@panda919
Copy link
Author

panda919 commented May 5, 2019

Actually, I didn't find solution for update config dynamically !!!
While running, even if i update config via setConfig(), token in header is not updated!!!

@mikehardy
Copy link
Contributor

did you try stop/config-with-reset/ready ? basically re-cycling the plugin ? I haven't tried it yet but this was going to be my test

@panda919
Copy link
Author

panda919 commented May 5, 2019

I didn't not stop.
Only reset and call sync().

@panda919
Copy link
Author

panda919 commented May 5, 2019

BackgroundGeolocation.setConfig({
headers: headers,
reset: true,
});
this.sync();

@christocracy
Copy link
Member

Yes, #setConfig. No need to #stop. Listen to #onHttp for 401 as your cue to change your token.

Be sure to do that in your HeadlessTask, as well.

@panda919
Copy link
Author

panda919 commented May 5, 2019

BackgroundGeolocation.onHttp(httpEvent => {
        //console.warn('[http] ', httpEvent.success, httpEvent.status);

        let statusCode = parseInt (parseInt(httpEvent.status) / 100 );
        if(statusCode == 4){

           ======================
                    // get new auth token
           =====================
                let headers = {
                  Accept: "application/json",
                  "Content-Type": "application/json",
                  Authorization: "Bearer " +--new token---
                };
                BackgroundGeolocation.setConfig({
                  headers: headers,
                  reset: true,
                });
                BackgroundGeolocation.sync();
      });

this is my source.
But , token is not updated, and i got 401 error yet.
What is wrong?!

@christocracy
Copy link
Member

(triple-backtick, with optional language)

let foo = "bar"

@panda919
Copy link
Author

panda919 commented May 5, 2019

thanks. but pls check above my code snipet. what is wrong?
I didn't get expected result for refresh token.

@christocracy
Copy link
Member

christocracy commented May 5, 2019

BackgroundGeolocation.setConfig({
  headers: headers,
  //reset: true,  // <-- reset doesn't apply to setConfig.  remove.
}).then((state) => {
  console.log('- Updated headers: ', state.headers);
  BackgroundGeolocation.sync(); 
});

The plugin's methods are asynchronous. You can't execute #sync until #setConfig completes.

@panda919
Copy link
Author

panda919 commented May 5, 2019

ah, got it.
I will try

@christocracy
Copy link
Member

This should also work:

BackgroundGeolocation.onHttp(httpEvent => async {
  await BackgroundGeolocation.setConfig({headers: headers});
  BackgroundGeolocation.sync();
});

@mikehardy
Copy link
Contributor

awesome this will help me too, and I likely would have also forgotten async - thanks @christocracy

@panda919
Copy link
Author

panda919 commented May 5, 2019

thanks very much

@christocracy
Copy link
Member

@mikehardy you can't change #extras of a location already persisted to plugin's SQLite db. Updated #extras will be applied to the next recorded location.

@mikehardy
Copy link
Contributor

mikehardy commented May 5, 2019

Yes - until you code up react-native-time-travel I'll have (from my perspective) incomplete data in the past but that's fine - with uids you only need one visible strobe to link things up and that's my use case, so works for me just fine

@1602
Copy link

1602 commented May 8, 2019

@christocracy when http sync happens in the background (without app running, swiped up to shut down), will onHttp hook still be triggered? I'm asking because I observe app sending outdated tokens over and over again.

As recommended in example from README I'm calling removeListeners() when component unmounts, perhaps it removes onHttp causing http to fail silently without token renewal logic to trigger.

What is recommended approach here?

@christocracy
Copy link
Member

swiped up to shut down), will onHttp hook still be triggered?

On Android, of course not. That's what Android Headless Mode is for.

@panda919
Copy link
Author

panda919 commented May 10, 2019

@christocracy
I have 3 question.

  • if i have to update config, I have to set {reset: true,} in first config object(meaning init config, not update config value.)
    -if i set schedule config, it is not necessary to call start() function, I may call only startSchedule() function?
    -if i call sync() function, schedule action will continue ?

@christocracy
Copy link
Member

christocracy commented May 10, 2019

if i have to update config, I have to set {reset: true,} in first config object(meaning init config, not update config value.)

In 3.0.4, reset now defaults totrue. You can forget about this now. #ready now always applies the supplied config.

#setConfig is unaware of reset. #reset is not a strict config option of the plugin API itself. It's implemented in the React Native Android code.

-if i set schedule config, it is not necessary to call start() function, I may call only startSchedule() function?

Correct. The Scheduler is a bot that automatically calls #start / #stop for you. Like a cron-task.

-if i call sync() function, schedule action will continue ?

Yes, of course. The HTTP Service is an independent entity that operates upon the SQLite database, selecting records, executing HTTP requests and deleting records when your server says 200.

@panda919
Copy link
Author

meaning, stopSchedule function call stop() function automatically implicty?
and startSchedule() call start() function automatically?
Because i got below guide.
http://prntscr.com/nmtq85

@christocracy
Copy link
Member

stopSchedule function call stop() function automatically implicty?

Why don't you experiment? No, it only stops the schedule bot.

and startSchedule() call start() function automatically?

#startSchedule starts the bot.

@panda919
Copy link
Author

I am meaning.

BackgroundGeolocation.start(() => {    
      BackgroundGeolocation.startSchedule();      
 });   
 BackgroundGeolocation.stopSchedule(() => {      
      BackgroundGeolocation.stop();     
 }); 

this code is correct?

@christocracy
Copy link
Member

If you have a schedule configured, why would you explicitly call #start? The schedule-bot will do that automatically according to your configured schedule.

As for the stopSchedule case, that's completely up to you. If the user were logging out, I'd certainly tell the plugin to #stop in that case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants