pCloud / pcloud-sdk-swift

The official pCloud Swift SDK for iOS and macOS for integration with the pCloud API.
MIT License
22 stars 7 forks source link

Properly detect that an app has been unlinked from an account #20

Closed john-work-ios closed 1 year ago

john-work-ios commented 1 year ago

I have been integrating the pCloud Swift SDK successfully. I'm testing for edge cases, when the user revokes access to the app, in his pCloud console. My problem: I still get a PCloud.sharedClient instance on setUp, and I only receive an error when the user tries to upload a file (or list a folder). How can I detect this before? Such that the user is being asked to authenticate before performing the upload/list folder/or any task.

Thanks!

tmpit commented 1 year ago

Hello. Other than calling an API method, there is no way for a client to detect that a token has been invalidated. The way I see it, there are two approaches to your problem:

  1. Poll the server periodically or at certain app events (e.g. willEnterForeground) to check the validity of the token. One simple way to do that would be to call the userinfo method (fetchUserInfo() on the client instance). This approach does not eliminate the problem, only mitigates it since your token can still be invalidated between the last time you checked its validity and the time the user attempts a file operation.
  2. React to the error when it happens, prompt the user to reauthenticate and retry the operation. Not the simplest approach, but it completely eliminates the problem, if it is possible for you.

The PCloud.sharedClient instance only requires authentication data in the keychain. That's intended. It's possible to add a utility method that checks the validity of the token before initializing the shared client. But I'm against doing that since it does very little to help with the problem.

john-work-ios commented 1 year ago

Thank you for this precious information! Much appreciated. I'll see what I can come up with based on that. Best regards