简体   繁体   English

在ionic中使用firebase登录facebook

[英]facebook login using firebase in ionic

I am facing problem to get email and likes of user after facebook login using firebase into my ionic app. 我在使用Firebase将Facebook登录到我的离子应用程序后,在获取电子邮件和用户喜欢的用户时遇到了问题。 Also after getting facebook display name and some other information in the user is not being redirected to the Dashboard page instead login page got refresh. 另外,在获取Facebook显示名称和用户中的某些其他信息后,用户名不会被重定向到“仪表板”页面,而是刷新了登录页面。 Although i read different posts on stackoverflow and other sorces but i am stuck. 虽然我阅读了关于stackoverflow和其他消息的不同文章,但是我被卡住了。 Please help me to solve these two issues. 请帮助我解决这两个问题。 **1)**How to get email and likes of user **2)**how to redirect user to dashboard soon after verified by facebook.. Here is the code ** 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);
})

Please make a constant to use it everywhere in your App like that 请像这样在应用程序中的任何地方使用常量

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

Then in the controller inject this constant "facebookUrl & "$state" as shown below 然后在控制器中注入此常量“ facebookUrl&“ $ state”,如下所示

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

and then you only need to give name of the state where you want to redirect after facebook authentication.. 然后您只需要提供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');
    }
  })
}})

and for email and likes you have to first make sure that these information is shown in the object of authData after successfull authentication .. 对于电子邮件和喜欢的邮件,您必须首先确保在成功身份验证后在authData对象中显示这些信息。

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

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

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