简体   繁体   English

通过Cordova / Angular / Ionic在手机上登录Facebook

[英]Facebook login on mobile via Cordova/Angular/Ionic

I'm working in a hybrid app to report potholes in our city. 我正在使用一个混合应用程序来报告我们城市的坑洼。 The user can register to the app in a classic way (fill forms) or via one of the social networks (facebook, gmail, twitter). 用户可以以传统方式(填写表格)或通过社交网络之一(facebook,gmail,twitter)注册到应用程序。

The system works through a server on rails and a mobile app as a client(ionic/angular) 该系统通过导轨上的服务器和作为客户端(离子/角度)的移动应用程序工作

On the server side we have solved this, the user can make sign up / sign in to the page in the way that they want. 在服务器端,我们已经解决了这个问题,用户可以按照自己的方式注册/登录页面。

But with have several problems with the app, the app does nothing when you make click to the button of "sign in via facebook" 但是由于该应用程序存在一些问题,因此当您单击“通过Facebook登录”按钮时,该应用程序不执行任何操作

this is the style it is organized. 这是它组织的样式。

app/
plugins/
    InAppBrowser
www/
    css/
    js/
        controllers/
            splash.js
            map.js
            tabs.js
        services/
            users.js
            notifications.js
        app.js
        utils.js
    lib/
        angular/
        ng-cordova-oauth/
        ngCordova/
        ionic/
    templates/
        map.html
        splash.html
        tabs.html
    index.html

The splash.js controller is in charge of making the login function. splash.js控制器负责执行登录功能。

angular.module('app')
.controller('SplashCtrl', function($scope, User, $state, $ionicPopup, $auth, $cordovaOauth) {
$scope.session_id = User.session_id;

$scope.facebookLogin = function() {
     alert("flag1");
    User.fbSignIn().then(function() {
         alert("flag2");
        User.fbGetData().then(function() {
             alert("flag3");
            User.fbAuth().then(function() {
                 alert("flag4");
                // Detect if it is a sign in or sign up
                if (User.username) {
                    console.log('Controller reports successfull social login.');
                    $state.go('tab.map');
                } else {
                    // Open finish signup modal
                    console.log('Contorller reports this is a new user');
                    $state.go('finish_signup');
                }
            }, function() {
                 alert("flag5");
                $ionicPopup.alert({
                    title: '<b>App</b>',
                    template: 'Credenciales no válidas, vuelve a intentar.',
                    okText: 'Aceptar',
                    okType: 'button-energized'
                })
            });
        }, function() {
             alert("flag6");
            // alert('Could not get your Facebook data...');
        });
    }, function() {
         alert("flag7");
        // alert('Could not sign you into Facebook...');
    });
}
})

I put some alert flags through the functions to see where the app get stuck. 我通过这些功能添加了一些警告标志,以查看应用程序卡在哪里。 I can only see the 'flag1' alert on the phone.Then nothing happens 我只能在电话上看到“ flag1”警报,然后什么也没有发生

the controller communicates with the service users.js I put the code on pastebin because it's too long users.js service 控制器与服务users.js通信我将代码放在pastebin上,因为users.js服务太长

The client must request an access token to the server and then compare in SplashCtrl if they got the token access the app redirects the user to tabs.html template that would be the main page. 客户端必须向服务器请求访问令牌,然后在SplashCtrl中进行比较(如果客户端获得了令牌访问权限),则应用会将用户重定向到作为主页的tabs.html模板。

The console server shows nothing. 控制台服务器什么也不显示。 So the request application never communicates to the server. 因此,请求应用程序永远不会与服务器通信。 Eventhough the 'CLIENTS' and 'SERVER' variables are already declared in app.js 尽管app.js中已经声明了“ CLIENTS”和“ SERVER”变量

.constant('SERVER', {

url: 'https://rails-tutorial-denialtorres.c9.io'
  })
.constant('CLIENTS', {
  facebook: 'fb Api'
  });

I can only logging of the server if I put a username and password in a traditional way preview 如果我以传统方式预览用户名和密码,则只能登录服务器

I hope you can help me with this guys 我希望你能帮我这个家伙

regards and thanks!! 问候和感谢!

you try this ? 你试试这个吗? http://ngcordova.com/docs/plugins/oauth/ http://ngcordova.com/docs/plugins/oauth/

I tested and work very well, easy to implement, and also you can parse the json with token (and use server side if you need) 我测试并运行得很好,易于实现,并且您还可以使用令牌解析json(并在需要时使用服务器端)

Remember this work ONLY with real device, not with Ionic Serve. 记住这项工作仅适用于真实设备,而不适用于Ionic Serve。

In case you looking for custom facebook login (javascript), try this code : 如果您要寻找自定义的Facebook登录名(javascript),请尝试以下代码:

facebookStatus = function() {

    var dfd = new jQuery.Deferred();

    FB.getLoginStatus(function(response) {

      if (response.status === 'connected') {
        dfd.resolve({status: response.status, token: response.authResponse.accessToken});
      } else if (response.status === 'not_authorized') {
        // the user is logged in to Facebook, //but not connected to the app
        dfd.resolve({status: response.status, token: false});
      } else {
        // the user isn't even logged in to Facebook.
        FB.login(function(response) {
          if (response.status=="connected"){
            var token = response.authResponse.accessToken;
            dfd.resolve({status: response.status, token: response.authResponse.accessToken});
          }
        }, {scope:'YOUR SCOPE HERE'});
      }

    });
    return dfd.promise();
  };

*Remember if you use this code, to add on index page the standard Facebook App details (something like) *请记住,如果您使用此代码,请在索引页面上添加标准的Facebook App详细信息(类似)

<div id="fb-root"></div>
<script>

    window.fbAsyncInit = function() {
        FB.init({
            appId      : 'YOUR APP ID',
            status     : true, // check login status
            cookie     : false, // enable cookies to allow the server to access the session
            xfbml      : true  // parse XFBML
        });

    };

    // Load the SDK asynchronously
    (function(d){
        var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
        if (d.getElementById(id)) {return;}
        js = d.createElement('script'); js.id = id; js.async = true;
        js.src = "//connect.facebook.net/en_US/all.js";
        ref.parentNode.insertBefore(js, ref);
    }(document));

</script>

When I install the cordova plugin with add org.apache.cordova.inappbrowser The id for the plugin is cordova-plugin-inappbrowser and the ngcordova.js library is looking for org.apache.cordova.inappbrowser 当我通过add org.apache.cordova.inappbrowser安装cordova插件时,该插件的ID为cordova-plugin-inappbrowser ,而ngcordova.js库正在寻找org.apache.cordova.inappbrowser

Changing those lines on ngcordova.js solves the issue. 更改ngcordova.js上的这些行即可解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM