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

token and client_id from InAppBrowser not persisted in ionic app on android #208

Closed
abhayastudios opened this issue Sep 6, 2015 · 8 comments

Comments

@abhayastudios
Copy link

After reading the comment of @nbrustein here, I started to better understand the communication between ng-token-auth and the inAppBrowser. If I print the response from ng-token-auth's handleLoadStop function to the console I see that it contains the full user object including the auth_token and client_id (which corresponds to the last client_id in the database). Therefore I think I can conclude that the communication with InBrowserApp works fine and that the ionic app has access to the data.

However, any subsequent API requests to my server receive a 401 Not Authorized response. When I look at the request headers of those API requests I don't see any of the headers that Chrome on my laptop is sending to the server such as: access-token, client, expiry, token-type and uid.

If I print to the console the key and value of the persistData function when running on android and with InAppBrowser mode then immediately after the callback I see the correct data, e.g.:

key: auth_headers
value: Object {access-token: "<token>", token-type: "Bearer", client: "<client>", expiry: "1442773182", uid: "<uid>"}

However, on subsequent requests I see this:

key: auth_headers
value: Object {}

I also see that it tries to save the auth_headers in a cookie because it enters the switch default of the persistData function. Therefore I assume that somehow saving data through ipCookie does not work properly on ionic/android.

@abhayastudios abhayastudios changed the title token and client_id from InAppBrowser not persisted in ionic app token and client_id from InAppBrowser not persisted in ionic app on android Sep 6, 2015
@abhayastudios
Copy link
Author

Woohoo I got it working! Turns out that when working with ionic and I assume it is the same with cordova, storing the tokens according to default behavior in the cookie does not work. However, once you configure it to work with localStorage it works great!

@dtelaroli
Copy link

@abhayastudios I'm still getting error, can you help me?

My screen freeze at the 'Redirecting...' screen.

I/dalvikvm( 1395): Could not find method android.webkit.WebView.evaluateJavascript, referenced from method org.apache.cordova.inappbrowser.InAppBrowser$3.run
W/dalvikvm( 1395): VFY: unable to resolve virtual method 223: Landroid/webkit/WebView;.evaluateJavascript (Ljava/lang/String;Landroid/webkit/ValueCallback;)V
D/dalvikvm( 1395): VFY: replacing opcode 0x6e at 0x002e
W/PluginManager( 1395): THREAD WARNING: exec() call to InAppBrowser.injectScriptCode blocked the main thread for 23ms. Plugin should use CordovaInterface.getThreadPool().
D/CordovaLog( 1395): file:///android_asset/www/cordova.js: Line 1058 : processMessage failed: Error: TypeError: Illegal constructor
I/Web Console( 1395): processMessage failed: Error: TypeError: Illegal constructor at file:///android_asset/www/cordova.js:1058
D/CordovaLog( 1395): file:///android_asset/www/cordova.js: Line 1059 : processMessage failed: Stack: TypeError: Illegal constructor
D/CordovaLog( 1395):     at file:///android_asset/www/lib/ng-token-auth/dist/ng-token-auth.js:353:24
D/CordovaLog( 1395):     at Object.callbackFromNative (file:///android_asset/www/cordova.js:292:54)
D/CordovaLog( 1395):     at processMessage (file:///android_asset/www/cordova.js:1053:21)
D/CordovaLog( 1395):     at Function.processMessages (file:///android_asset/www/cordova.js:1090:13)
D/CordovaLog( 1395):     at pollOnce (file:///android_asset/www/cordova.js:955:17)
D/CordovaLog( 1395):     at pollOnceFromOnlineEvent (file:///android_asset/www/cordova.js:945:5)
I/Web Console( 1395): processMessage failed: Stack: TypeError: Illegal constructor
I/Web Console( 1395):     at file:///android_asset/www/lib/ng-token-auth/dist/ng-token-auth.js:353:24
I/Web Console( 1395):     at Object.callbackFromNative (file:///android_asset/www/cordova.js:292:54)
I/Web Console( 1395):     at processMessage (file:///android_asset/www/cordova.js:1053:21)
I/Web Console( 1395):     at Function.processMessages (file:///android_asset/www/cordova.js:1090:13)
I/Web Console( 1395):     at pollOnce (file:///android_asset/www/cordova.js:955:17)
I/Web Console( 1395):     at pollOnceFromOnlineEvent (file:///android_asset/www/cordova.js:945:5) at file:///android_asset/www/cordova.js:1059
D/CordovaLog( 1395): file:///android_asset/www/cordova.js: Line 1060 : processMessage failed: Message: S01 InAppBrowser1271410129 [{"uid":"116642106272838797656","nickname":null,"image":"https:\/\/lh6.googleusercontent.com\/-oYjG0jh0_uY\/AAAAAAAAAAI\/AAAAAAAAAAA\/ENPxne79kt4\/photo.jpg?sz=50","auth_token":"ZeszUYzOT3PrzqhtevxWQA","provider":"google_oauth2","expiry":1443043019,"message":"deliverCredentials","id":1,"updated_at":"2015-09-09T21:16:59.955Z","email":"[email protected]","name":"Denilson Silva","config":null,"created_at":"2015-09-08T16:56:28.993Z","client_id":"JZ2Cfhb6o4m4hNNBiAcBLg"}]
I/Web Console( 1395): processMessage failed: Message: S01 InAppBrowser1271410129 [{"uid":"116642106272838797656","nickname":null,"image":"https:\/\/lh6.googleusercontent.com\/-oYjG0jh0_uY\/AAAAAAAAAAI\/AAAAAAAAAAA\/ENPxne79kt4\/photo.jpg?sz=50","auth_token":"ZeszUYzOT3PrzqhtevxWQA","provider":"google_oauth2","expiry":1443043019,"message":"deliverCredentials","id":1,"updated_at":"2015-09-09T21:16:59.955Z","email":"[email protected]","name":"Denilson Silva","config":null,"created_at":"2015-09-08T16:56:28.993Z","client_id":"JZ2Cfhb6o4m4hNNBiAcBLg"}] at file:///android_asset/www/cordova.js:1060

@dtelaroli
Copy link

I created a new application and works.
Thanks

@aL3xa
Copy link

aL3xa commented Sep 18, 2015

I have moved auth_header storage from cookies to localStorage, but I still get the freeze on Redirecting... page. @dtelaroli, can you please put some sample code to see how you handle authentication? FTR, I'm using plain Cordova, not Ionic.

@dtelaroli
Copy link

@aL3xa To fix the freeze I increased the timeout in the devise view https://github.com/lynndylanhurley/devise_token_auth/blob/master/app/views/devise_token_auth/omniauth_external_window.html.erb.

Create the view in your project but with 3000 milliseconds to test (line 31).
Folder: app/views/devise_token_auth
File: omniauth_external_window.html.erb

@aL3xa
Copy link

aL3xa commented Sep 21, 2015

@dtelaroli, thanks for the update, but I still get the freeze after increasing the timeout value. Apparently, message event is not being fired at all, hence the page is stuck on Redirecting.... I have only these Cordova-specific JS lines:

'use strict';

document.addEventListener('deviceready', function () {
  window.open = window.cordova.InAppBrowser.open;
  console.log('console.log works well');
}, false);

The Angular config section is pretty straightforward:

  .config(function($authProvider, ENV){
    $authProvider
      .configure({
        apiUrl: ENV.apiEndpoint,
        omniauthWindowType: 'inAppBrowser',
        storage: 'localStorage',
        authProviderPaths: {
          google:   '/auth/google_oauth2',
          facebook: '/auth/facebook',
          twitter: '/auth/twitter',
          osm: '/auth/osm'
        }
      });
  })

Any ideas what may have caused "the freeze"?

@dtelaroli
Copy link

@aL3xa is it working if you debug at chrome? Can you find the logcat and chrome console for errors?

I configured for tests in android emulator and chrome (If your code is working fine at chrome the problema is confirmed at inAppBrowser).

It's possible test outside emulator to easy debugging (it's required configure your api account to localhost). I use the ionic serve to do that:

$authProvider.configure({
      omniauthWindowType: window.cordova == undefined ? 'newWindow' : 'inAppBrowser',
      storage: 'localStorage'
  });

Can you share one small project with your code connecting to an api test server?

@aL3xa
Copy link

aL3xa commented Sep 21, 2015

I haven't tried it in Chrome (yet), as I'm debugging on android v4.1.x, but weinre returns something that might come handy:

Error in Success callbackId: InAppBrowser1226996874 : TypeError: Illegal constructor

Update: I can confirm the bug on vanilla Cordova projects. I filed this one at #214.
@dtelaroli do you have any working Ionic sample code that you can share?

diraulo pushed a commit to CraftAcademyLabs/adventure-map-mobile that referenced this issue Feb 4, 2017
* merges with upstream
* modifies gitignore
* adds allow-navigation and modifies the way we use inAppBrowser
stucj on redirect see lynndylanhurley/ng-token-auth#208

* adds scenario for logging in with facebook - no error handling
* minor refactoring
* generates resources to run emulator
* cleans unused code in activitiesController
* reverts back to use http and not https as api protocol for auth related requests
* removes unused allow-navigation setting in config.xml
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

3 participants