Fazil
Fazil

Reputation: 1390

Inappbrowser callback

I have tried to call back after login in facebook using inappbrowser. But it doesnt work to check the email,name etc.Here my code

Inappbrowser calling

function onDeviceReady(){   
     var my_client_id = FBkey,
     my_redirect_uri = "http://www.fastabuy.com/index.php",
     my_type = "user_agent",
     my_display = "touch";

     var authorize_url = FBgraphapiurl+"/oauth/authorize?";
     authorize_url += "client_id=" + my_client_id;
     authorize_url += "&redirect_uri=" + my_redirect_uri;
     authorize_url += "&display=" + my_display;
     authorize_url += "&scope=publish_stream,email,user_likes";
     isfir = "true";

     var ref = window.open(authorize_url, '_blank', 'location=yes');
     ref.addEventListener('loadstop', function facebookLocChanged() {
         facebookLoc(my_redirect_uri)
     });
}

Method for checking call back

  function facebookLoc(loc){
     if (loc.indexOf("http://www.fastabuy.com/index.php?") > -1){
       alert(loc.indexOf)
     }
  }

How to check when the url loc.indexOf>-1 to close the inappbrowser. Please help me to sort this out.

Upvotes: 3

Views: 6421

Answers (2)

Raul Duran
Raul Duran

Reputation: 548

Here a sample https://github.com/raulduran/facebook-cordova

onDeviceReady: function() {
    app.onFacebookLogin();
},

onFacebookLogin: function() {

    var authorize_url  = "https://m.facebook.com/dialog/oauth?";
        authorize_url += "client_id=" + appId;
        authorize_url += "&redirect_uri=" + redirectUrl;
        authorize_url += "&display=touch";
        authorize_url += "&response_type=token";
        authorize_url += "&type=user_agent";

    if(permissions !== '') {
        authorize_url += "&scope=" + permissions;
    }

    var userDenied = false;
    var appInBrowser = window.open(authorize_url, '_blank', 'location=no');

    appInBrowser.addEventListener('loadstart', function(location) {

        if (location.url.indexOf("access_token") !== -1) {
            // Success
            var access_token = location.url.match(/access_token=(.*)$/)[1].split('&expires_in')[0];
            window.localStorage.setItem('facebook_accessToken', access_token);
            appInBrowser.close();
        }

        if (location.url.indexOf("error_reason=user_denied") !== -1) {
            // User denied
            userDenied = true;
            window.localStorage.setItem('facebook_accessToken', null);
            appInBrowser.close();
        }
    });
}

Upvotes: 4

Viswa
Viswa

Reputation: 3211

1) You need to use loadstart event in InAppBrowser for checking redirected page.

2) After successful login use close method for closing InAppBrowser.

3) When you call facebookLoc function in callback, you should watch out for scope and i am using me variable to solve this problem.

onFacebookLogin: function() {

    var me = this;
    var appInBrowser = window.open(authorize_url, '_blank', 'location=yes');

    appInBrowser.addEventListener('loadstart', function(event) {
         me.facebookLoc(event.url,appInBrowser);
    });    
}

facebookLoc : function(loc,appInBrowser) {
   if (loc.indexOf("www.facebook.com/connect/login_success.html") > -1){
         alert('Login success');
         appInBrowser.close();
   }
}

Upvotes: 7

Related Questions