簡體   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