remotestorage / remotestorage.js

⬡ JavaScript client library for integrating remoteStorage in apps
https://remotestoragejs.readthedocs.io
MIT License
2.32k stars 141 forks source link

Error on authorization for Cordova app #875

Closed lewisl9029 closed 9 years ago

lewisl9029 commented 9 years ago

Using 11.2 on a Cordova Android app, I get the following exception when connecting to a @5apps.com remoteStorage account (tested to be working perfectly in Chrome):

Possibly unhandled TypeError: Cannot read property '0' of null
    at Object.RemoteStorage.authorize (file:///android_asset/www/app.js:15226:65)
    at Object.<anonymous> (file:///android_asset/www/app.js:13783:22)
    at tryCatch1 (file:///android_asset/www/app.js:13416:25)
    at Promise$_callHandler [as _callHandler] (file:///android_asset/www/app.js:12513:21)
    at Promise$_settlePromiseFromHandler [as _settlePromiseFromHandler] (file:///android_asset/www/app.js:12523:28)
    at Promise$_settlePromiseAt [as _settlePromiseAt] (file:///android_asset/www/app.js:12653:22)
    at Promise$_settlePromises [as _settlePromises] (file:///android_asset/www/app.js:12779:22)
    at Async$_consumeFunctionBuffer [as _consumeFunctionBuffer] (file:///android_asset/www/app.js:11309:18)
    at Async$consumeFunctionBuffer (file:///android_asset/www/app.js:11281:20)
    at MutationObserver.Promise$_Scheduler (file:///android_asset/www/app.js:13246:17)

The line responsible appears to be in authorize.js: https://github.com/remotestorage/remotestorage.js/blob/acec51c86d8b132bc8779f95eb686cddfb20ac9a/src/authorize.js#L50

var redirectUri = String(RemoteStorage.Authorize.getLocation()); 
var clientId = redirectUri.match(/^(https?:\/\/[^\/]+)/)[0];

redirectUri evaluates to "file:///android_asset/www/index.html#/app/cloud" in this case, so the regex used here fails to match anything. I imagine there will be similar errors on other Cordova platforms, but I don't really have any other devices to test with at the moment.

raucao commented 9 years ago

Yes, without an actual origin, we have a general problem with the OAuth redirect flow being insufficient for just using it directly like that.

I think nobody else has integrated rs.js in a Cordova app so far, although there are plans for e.g. a sync app that would sync device contacts, calendars etc to and from a remote storage.

I'm not sure how other people solve this, when they only have the redirect flow to work with, but I do vaguely remember some apps using it in the past. That's definitely the place to start looking: how do people use OAuth implicit grant in native apps?

lewisl9029 commented 9 years ago

Thanks for the quick reply. That makes sense.

This is my first time using OAuth on Cordova, so I wasn't aware of the complications.

I found this article that seems to offer a solution using the inappbrowser plugin, a dummy redirect URI, and extracts the OAuth tokens from the browser URL when OAuth completes: http://phonegap-tips.com/articles/google-api-oauth-with-phonegaps-inappbrowser.html

I'll try implementing something like this for remoteStorage. In the mean time, if anyone has anything they'd like to share regarding using remoteStorage within Cordova, I'd love to hear them.

raucao commented 9 years ago

Ah, yes. That sounds like what I remembered.

So, as the user will otherwise see your app as localhost, here's another idea for you: you could use an app-specific domain that points to localhost. E.g. @galfert set up *.lolcathost.de to point to 127.0.0.1 for development purposes (making it easy to have different origins for different apps on localhost so that IndexedDB and localStorage aren't messed up).

So, in this case something like android.yourapp.com could point to 127.0.0.1 and give that as redirect URI to the RS server.

raucao commented 9 years ago

@lewisl9029 Did you make any progress on this so far? I'd be super interested in a solution, because it means we could sync all kinds of data from and to phones via a single API (so we can get to e.g. all contacts or calendar entries from all the different services at once).

Let me know if I can help somehow!

lewisl9029 commented 9 years ago

I've been working on a few other features lately. I plan to start on this later this week.

I'll definitely let you know if I make any progress or run into anything I need help with. =)

raucao commented 9 years ago

Cool! Looking forward to it. Thanks for the quick update.

lewisl9029 commented 9 years ago

I managed to get it working. =) Please take a look at #880 when you have the time.

raucao commented 9 years ago

w00t!

raucao commented 9 years ago

Cordova support, contributed by @lewisl9029, just landed in master. \o/