StasDoskalenko / react-native-google-fit

A React Native bridge module for interacting with Google Fit
MIT License
333 stars 209 forks source link

GoogleFit.checkIsAuthorized() is not working #240

Closed Veervijay38 closed 3 years ago

Veervijay38 commented 3 years ago

For Authorization for the first time i am calling following function const options = { scopes: [ Scopes.FITNESS_ACTIVITY_READ, Scopes.FITNESS_ACTIVITY_WRITE, Scopes.FITNESS_BODY_READ, Scopes.FITNESS_BODY_WRITE, ], } GoogleFit.authorize(options) .then(authResult => { if (authResult.success) { dispatch("AUTH_SUCCESS"); } else { dispatch("AUTH_DENIED", authResult.message); } }) .catch(() => { dispatch("AUTH_ERROR"); })

==> After Successful authentication on second page for checking i am calling following function

GoogleFit.checkIsAuthorized().then(() => { console.log(GoogleFit.isAuthorized) // Then you can simply refer to GoogleFit.isAuthorized boolean. })

if app is running continuously after first authentication than this GoogleFit.checkIsAuthorized() funtion is giving GoogleFit.isAuthorized=true

But if i close my app and than again open the app than its not giving me GoogleFit.isAuthorized=true & all the step history is also disappears

Please Help me with this problem !

aboveyunhai commented 3 years ago

I believe you need to call the Auth API every time when you reopen your app. But the process will be automate after your first login. So you will not see the authentication screen like the first time.

https://github.com/aboveyunhai/FitEtc/blob/851aa34e57705199997544f9f65cc99f55540f45/src/redux/actions/ActionCreator.tsx#L20

This is how I handle the auth process in my own test app. If you check all other functions related to Google Fit, I call the auth() in front of every individual Fit function.

I had heard people said the checkIsAuthorized() is not reliable in the past, but I cannot confirm.

Abdulmalick-Dimnang commented 3 years ago

@aboveyunhai I have a concern, with your flow, this means that you will have to cal authorize every time the app opens but what if I want to get the authorization status without calling. Lets say I have a button that does for the authorization at first, and after authorization this button is disabled since I have an authorized user. After that, i close and reopen the app, this button is technically "not disabled" since GoogleFit.isAuthorized will be false and in order to get it to true, I will have to follow your flow. But now, what if I'm a first user and I want to see the consent screen once I click on the button of authorize, on this case it will not work with your flow.

Is there a reason why GoogleFit.isAuthorized of GoogleFit.CheckIsAuthorized returns false after reopen the app?

aboveyunhai commented 3 years ago

@Abdulmalick-Dimnang You can just call auth inside the button without any further action, if you are logout or new, it will auto trig the consent screen. So there is not difference between new or old except the old one will do nothing visually since you are already login.

To your second question: Implementation issue itself is a problem, that's why I try to migrate the deprecated Auth flow. So it will make those two functions more reliable at least. You can think of it as a Global Context value in redux/react context but currently lack of a reliable persistent storage. So after you reopen, it becomes to it's default value, which it's false;

Even though they work correctly in the future, the reason why I emphasize you should never rely on them is because you never know what will happen inside or outside the app . For example, somehow your app access on Google Fitness banned or remove by users/devices/other reasons at some point but they are not aware of.

If you rely on that login status check. you can't really tell if it's true or no in many cases. So the auth guard we are talking about will ensure the consent screen re-appear.

Google Fit Api is smart enough to make the consent screen only appear once even if you recall the auth again until you are logout, or scope changing. It will auto check against the existing account instead of login twice based on my own understanding.