繁体   English   中英

在ionic中使用firebase登录facebook

[英]facebook login using firebase in ionic

我在使用Firebase将Facebook登录到我的离子应用程序后,在获取电子邮件和用户喜欢的用户时遇到了问题。 另外,在获取Facebook显示名称和用户中的某些其他信息后,用户名不会被重定向到“仪表板”页面,而是刷新了登录页面。 虽然我阅读了关于stackoverflow和其他消息的不同文章,但是我被卡住了。 请帮助我解决这两个问题。 ** 1)**如何获取用户的电子邮件和喜欢的邮件** 2)**如何在经过Facebook验证后立即将用户重定向到信息中心。这是代码

****Controller:****

$scope.loginFaceBook = function() {
        ///alert('fb Login firebase...');
        var existingAuthData = Auth.$getAuth();
        console.log("existingAuthData : ",existingAuthData);
        //console.log("existingAuthData Email : ",existingAuthData.thirdPartyUserData.email);
       Auth.$authWithOAuthRedirect("facebook").then(function(authData) {
            alert('done 1');
      // User successfully logged in
           console.log(authData);
           $location.path('app/Dash');
    }, {
  remember: "sessionOnly",
  scope: "email,user_likes"
}).catch(function(error) {
      if (error.code === "TRANSPORT_UNAVAILABLE") {
        Auth.$authWithOAuthPopup("facebook").then(function(authData) {
          // User successfully logged in. We can log to the console
          // since we’re using a popup here
            alert('done 2');
          console.log(authData);
        $location.path('app/Dash');
        }, {
  remember: "sessionOnly",
  scope: "email,user_likes"
});
      } else {
        // Another error occurred
        console.log(error);
            alert('err');
      }
    });

Auth.$onAuth(function(authData) {
  if (authData === null) {
    console.log("Not logged in yet");
  } else {
    console.log("Logged in as", authData.uid);
      console.log("Details",authData);
      console.log("Name:",authData.facebook.displayName);
        alert("Name:",authData.facebook.displayName);
       console.log("gender:",authData.facebook.cachedUserProfile.gender);
      console.log(" Email : ",authData.facebook.email);

      $location.path('app/Dash');
  }
  $scope.authData = authData; // This will display the user's name in our view
});

    };

**App.js**

// Ionic Starter App


// 'starter.controllers' is found in controllers.js
angular.module('starter', ['ionic', 'starter.controllers', 'ngCordovaOauth','firebase','ngFileUpload'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if (window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);

    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }
  });
})

.factory("Auth", function($firebaseAuth) {
  var usersRef = new Firebase("https//*********.firebase***.****/****");
  return $firebaseAuth(usersRef);
})

请像这样在应用程序中的任何地方使用常量

.constant('facebookUrl', 'https://rjrestaurantapp.firebaseio.com');

然后在控制器中注入此常量“ facebookUrl&“ $ state”,如下所示

.controller('loginCtrl', function($scope,facebookUrl,$state){

然后您只需要提供Facebook身份验证后要重定向的州的名称即可。

var ref = new Firebase(facebookUrl);
$scope.fbLogin = function () {
  ref.authWithOAuthPopup("facebook", function(error, authData) {
    if (error) {
      console.log("Login Failed!", error);
    } else {
      console.log("Authenticated successfully with payload:", authData);
      $state.go('restaurant');
    }
  })
}})

对于电子邮件和喜欢的邮件,您必须首先确保在成功身份验证后在authData对象中显示这些信息。

请仔细阅读此文档https://www.firebase.com/docs/web/guide/login/facebook.html

暂无
暂无

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

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