简体   繁体   中英

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. 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. Although i read different posts on stackoverflow and other sorces but i am stuck. 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

****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

.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..

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 ..

please read this doc carefully https://www.firebase.com/docs/web/guide/login/facebook.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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