doomsower / react-native-vkontakte-login

React native wrapper around VK iOS/Adnroid SDK
MIT License
98 stars 44 forks source link

iOS isLoggedIn always false #40

Open frmrgr opened 6 years ago

frmrgr commented 6 years ago

isLoggedIn method always resolves with false on ios after app restart despite successful authorization. I do not know much about how sdk works, but it seems that wakeUpSession call is required for sdk to restore accessToken. At least, I cannot trace (sdk code) how initializeWithAppId (which is the only thing called by this library on app launch) can set vkSdkInstance.accessToken (which is how isLoggedIn works) without calling anything else.

doomsower commented 6 years ago

Yeah it looks like Android counterpart does this. I use this module to login into my own backend and then use its credentials, so I didn't have a chance to notice this.

The difference is that iOS needs permissions scope to wake up, and Android obtains it from current access token (if it exists). Therefore, optional permissions argument should be added to initialize method of this module. It should be optional to be a non-breaking change, and it's not needed on android anyway. In case if permissions are not provided, iOS bridge should try to get current access token (as android does) and use its permissions to call wakeUpSession.

This shouldn't be difficult, but I don't have time for this at least until Monday. I can review the PR though.

frmrgr commented 6 years ago

I think that permissions argument is unnecesary, since it is only used to check against already present scope and force logout in case something is missing. Long story short, passing empty array should be just fine if the purpose is to just check whether user is logged. So basically, as I see it, it would be

[VKSdk wakeUpSession:@[] completeBlock:^(VKAuthorizationState state, NSError *error) {
  if (error)
    reject(error);
  else
    resolve([NSNumber numberWithBool:[VKSdk isLoggedIn]]);
}

Sorry, I don't wanna do PR since I am not objc programmer.

doomsower commented 6 years ago

Yeah you're right, it's ok to call wakeUpSession with empty array. But semantically wakeUpSession should be called in initialize, and vk-android-sdk does so internally. Also isLoggedIn works similarly in vk-ios-sdk and vk-android-sdk.

Then vk-ios-sdk docs say that wakeUpSession can result in

VKAuthorizationError - means some error happened when we tried to check the authorization. Probably, the internet connection has a bad quality. You have to try again later.

So I need to check what android does in this situation. So my guess is that it does nothing, and if session is not waken on initialize due to error, then isLoggedIn is false.

So when I'm going to work on this the plan is:

  1. Do some experiments and compare ios and android, check what android does if wakeUpSession in initialize fails. Then maybe I'll change my mind about 2 and 3
  2. wakeUpSession should be called on ios initialize with empty array and with no-op completeBlock.
  3. wakeUpSession should be exposed to JS
doomsower commented 6 years ago

I just published react-native-vkontakte-login@0.4.0-beta.2
In this version wakeUpSession is called during initialize on iOS.
I also added getAccessToken method.

Please test it.