0

您好我發現了一個使用angularfire進行路由的auth示例,我只是將其更改爲支持新的firebase sdk v4並仍然使用angularfire v1。angularfire和Facebook登錄獲取無法讀取undefined屬性'onAuth'

這是一段代碼我用的鏈路(具有UI的路由器): angularfire docs

現在這是我的app.js和index.html

var config = { 
 
    "apiKey": "AIzaSyAUoM0RYqF1-wHI_kYV_8LKgIwxmBEweZ8", 
 
    "authDomain": "clubears-156821.firebaseapp.com", 
 
    "databaseURL": "https://clubears-156821.firebaseio.com", 
 
    "projectId": "clubears-156821", 
 
    "storageBucket": "clubears-156821.appspot.com", 
 
    "messagingSenderId": "970903539685" 
 
}; 
 
firebase.initializeApp(config); 
 

 
var app = angular.module("sampleApp", [ 
 
    "firebase", 
 
    "ui.router" 
 
]); 
 

 
app.factory("Auth", ["$firebaseAuth", 
 
    function ($firebaseAuth) { 
 
     return $firebaseAuth(); 
 
    } 
 
]); 
 

 

 
// UI.ROUTER STUFF 
 
app.run(["$rootScope", "$state", function ($rootScope, $state) { 
 
     $rootScope.$on("$stateChangeError", function (event, toState, toParams, fromState, fromParams, error) { 
 
      if (error === "AUTH_REQUIRED") { 
 
       $state.go("home"); 
 
      } 
 
     }); 
 
    }]); 
 
app.config(function ($stateProvider, $urlRouterProvider) { 
 
    $urlRouterProvider.otherwise("/home"); 
 
    $stateProvider 
 
      .state('home', { 
 
       url: "/home", 
 
       template: "<h1>Home</h1><p>This is the Home page</p>", 
 
       resolve: { 
 
        "currentAuth": ["Auth", function (Auth) { 
 
          return Auth.$waitForAuth(); 
 
         }] 
 
       } 
 
      }) 
 
      .state('profile', { 
 
       url: "/profile", 
 
       template: "<h1>Profile</h1><p>This is the Profile page</p>", 
 
       resolve: { 
 
        "currentAuth": ["Auth", function (Auth) { 
 
          return Auth.$requireSignIn(); 
 
         }] 
 
       } 
 
      }) 
 
      }); 
 

 
    app.controller("MainCtrl", ["$scope", "Auth", 
 
     function ($scope, Auth) { 
 
      $scope.auth = Auth; 
 
      console.log(Auth); 
 
    $scope.auth.$onAuth(function(authData) { 
 
     $scope.authData = authData; 
 
     console.log(authData); 
 
    }); 
 
     } 
 
    ]); 
 
    app.controller("NavCtrl", ["$scope", "Auth", 
 
     function ($scope, Auth) { 
 
      $scope.auth = Auth; 
 
      console.log(Auth); 
 
    $scope.auth.$onAuth(function(authData) { 
 
     $scope.authData = authData; 
 
    }); 
 
     } 
 
    ]);
<!DOCTYPE html> 
 
<!-- 
 
To change this license header, choose License Headers in Project Properties. 
 
To change this template file, choose Tools | Templates 
 
and open the template in the editor. 
 
--> 
 
<html> 
 
    <head> 
 
     <title>TODO supply a title</title> 
 
     <meta charset="UTF-8"> 
 
     <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
 
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> 
 
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css"> 
 
     
 
    </head> 
 
    <body> 
 
     <div ng-app="sampleApp"> 
 
      <div ng-controller="MainCtrl"> 
 
       <nav class="navbar navbar-default navbar-static-top" ng-controller="NavCtrl"> 
 
        <div class="container"> 
 
         <div class="navbar-header"> 
 
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar"> 
 
           <span class="sr-only">Toggle navigation</span> 
 
           <span class="icon-bar"></span> 
 
           <span class="icon-bar"></span> 
 
           <span class="icon-bar"></span> 
 
          </button> 
 
          <a class="navbar-brand" href="home">Project name</a> 
 
         </div> 
 
         <div id="navbar" class="navbar-collapse collapse"> 
 
          <ul class="nav navbar-nav"> 
 
           <li ui-sref-active="active"> 
 
            <a ui-sref="home" href="#">Home</a> 
 
           </li> 
 
           <li ui-sref-active="active" ng-show="authData"> 
 
            <a ui-sref="profile" href="#"> 
 
             My Profile 
 
            </a> 
 
           </li> 
 
          </ul> 
 
          <ul class="nav navbar-nav navbar-right"> 
 
           <li ng-hide="authData"> 
 
            <a href="#" ng-click="$parent.auth.$authWithOAuthPopup('facebook')"> 
 
             <span class="fa fa-facebook-official"></span> 
 
             Sign In with Facebook 
 
            </a> 
 
           </li> 
 
           <li ng-show="authData"> 
 
            <a href="#" ng-click="$parent.auth.$unauth()"> 
 
             <span class="fa fa-sign-out"></span> 
 
             Logout 
 
            </a> 
 
           </li> 
 
          </ul> 
 
         </div> 
 
         <!--/.nav-collapse --> 
 
        </div> 
 
       </nav> 
 

 
       <div class="container"> 
 
        <div ui-view ng-show="authData"></div> 
 
        <div class="login-screen" ng-hide="authData"> 
 
         <div class="jumbotron text-center"> 
 
          <h1>Sweet login, brah.</h1> 
 
          <p class="lead">This is a pretty simple login utilizing <a href="https://angularjs.org/">AngularJS</a> and <a href="https://www.firebase.com/docs/web/libraries/angular/">AngularFire</a>.</p> 
 
          <button class="btn btn-primary btn-lg" ng-click="auth.$authWithOAuthPopup('facebook')"> 
 
           <span class="fa fa-facebook-official fa-fw"></span> 
 
           Sign in with Facebook 
 
          </button> 
 
         </div> 
 
        </div> 
 
       </div> 
 
      </div> 
 
     </div> 
 
     
 
     
 
     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
 
     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> 
 
     <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.14/angular.min.js"></script> 
 
    
 
     <script src="https://www.gstatic.com/firebasejs/4.0.0/firebase.js"></script> 
 
      <script src="app.js"></script> 
 
     <script src="https://cdn.firebase.com/libs/angularfire/1.1.3/angularfire.min.js"></script> 
 
     <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.18/angular-ui-router.min.js"></script> 
 
    
 
    </body> 
 
    
 
    
 
</html>

現在的問題是,我得到一個錯誤:無法讀取未定義的屬性'onAuth'。 我認爲它的新SDk的版本的問題,我在這裏提出的解決方案在stackoverflow中查找,但沒有人解決了我的問題。

請幫助...

回答

0

好吧,我找出問題,並通過改變角度和angularfire版本修復它。並稍稍改變遷移到新的SDK。

這是新代碼。

我現在的問題是,我沒有得到我想要從facebook的所有範圍。例如我想要生日,我看不到它回來。

有人有建議嗎?

var config = { 
 
    "apiKey": "AIzaSyAUoM0RYqF1-wHI_kYV_8LKgIwxmBEweZ8", 
 
    "authDomain": "clubears-156821.firebaseapp.com", 
 
    "databaseURL": "https://clubears-156821.firebaseio.com", 
 
    "projectId": "clubears-156821", 
 
    "storageBucket": "clubears-156821.appspot.com", 
 
    "messagingSenderId": "970903539685" 
 
}; 
 
firebase.initializeApp(config); 
 

 
var app = angular.module("sampleApp", [ 
 
    "firebase", 
 
    "ui.router" 
 
]); 
 

 
app.factory("Auth", ["$firebaseAuth", 
 
    function ($firebaseAuth) { 
 
     return $firebaseAuth(); 
 
    } 
 
]); 
 

 
//var provider = Auth.FacebookAuthProvider(); 
 
//provider.addScope('user_birthday'); 
 
// 
 
//Auth.signInWithRedirect(provider); 
 

 

 
// UI.ROUTER STUFF 
 
app.run(["$rootScope", "$state", function ($rootScope, $state) { 
 
     $rootScope.$on("$stateChangeError", function (event, toState, toParams, fromState, fromParams, error) { 
 
      if (error === "AUTH_REQUIRED") { 
 
       $state.go("home"); 
 
      } 
 
     }); 
 
    }]); 
 
app.config(function ($stateProvider, $urlRouterProvider) { 
 
    $urlRouterProvider.otherwise("/home"); 
 
    $stateProvider 
 
      .state('home', { 
 
       url: "/home", 
 
       template: "<h1>Home</h1><p>This is the Home page</p>", 
 
       resolve: { 
 
        "currentAuth": ["Auth", function (Auth) { 
 
          return Auth.$waitForSignIn(); 
 
         }] 
 
       } 
 
      }) 
 
      .state('profile', { 
 
       url: "/profile", 
 
       template: "<h1>Profile</h1><p>This is the Profile page</p>", 
 
       resolve: { 
 
        "currentAuth": ["Auth", function (Auth) { 
 
          return Auth.$requireSignIn(); 
 
         }] 
 
       } 
 
      }); 
 
}); 
 

 
app.controller("MainCtrl", ["$scope", "Auth", 
 
    function ($scope, Auth) { 
 
     $scope.auth = Auth; 
 
     console.log(Auth); 
 

 

 

 

 
     $scope.auth.$onAuthStateChanged(function (authData) { 
 
      $scope.authData = authData; 
 
      console.log(authData); 
 
     }); 
 
    } 
 
]); 
 

 

 

 

 
app.controller("NavCtrl", ["$scope", "Auth", 
 
    function ($scope, Auth) { 
 

 
     $scope.currentUser = null; 
 
     $scope.currentUserRef = null; 
 
     $scope.currentLocation = null; 
 

 
     $scope.auth = Auth; 
 
     console.log(Auth); 
 

 
     /** 
 
     * Function called when clicking the Login/Logout button. 
 
     */ 
 
     // [START buttoncallback] 
 
     $scope.SignIn = function() { 
 
      if (!Auth.currentUser) { 
 

 
       $scope.auth.$signInWithRedirect('facebook', { 
 
        scope: 'email, public_profile, user_birthday' 
 
       }).then(function (authData) { 
 
        // never come here handle in $onAuthStateChanged because using redirect method 
 
       }).catch(function (error) { 
 
        if (error.code === 'TRANSPORT_UNAVAILABLE') { 
 
         $scope.$signInWithPopup('facebook', { 
 
          scope: 'email, public_profile, user_friends' 
 
         }).catch(function (error) { 
 
          console.error('login error: ', error); 
 
         }); 
 
        } else { 
 
         console.error('login error: ', error); 
 
        } 
 
       }); 
 

 
      } else { 
 
       // [START signout] 
 
       Auth.signOut(); 
 
       // [END signout] 
 
      } 
 

 
     }; 
 
     // [END buttoncallback] 
 

 
// 
 
//  $scope.updateUserData = function() { 
 
//   $scope.currentUserRef.set($scope.currentUser); 
 
//  }; 
 

 

 
     $scope.auth.$onAuthStateChanged(function (authData) { 
 
      $scope.authData = authData; 
 
      
 
      console.log('after login'); 
 
      console.log($scope.authData); 
 
     }); 
 
    } 
 
]);
<!DOCTYPE html> 
 
<!-- 
 
To change this license header, choose License Headers in Project Properties. 
 
To change this template file, choose Tools | Templates 
 
and open the template in the editor. 
 
--> 
 
<html> 
 
    <head> 
 
     <title>TODO supply a title</title> 
 
     <meta charset="UTF-8"> 
 
     <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
 
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> 
 
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css"> 
 
     <link rel="icon" href="data:;base64,iVBORw0KGgo="> 
 
    </head> 
 
    <body> 
 
     <div ng-app="sampleApp"> 
 
      <div ng-controller="MainCtrl"> 
 
       <nav class="navbar navbar-default navbar-static-top" ng-controller="NavCtrl"> 
 
        <div class="container"> 
 
         <div class="navbar-header"> 
 
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar"> 
 
           <span class="sr-only">Toggle navigation</span> 
 
           <span class="icon-bar"></span> 
 
           <span class="icon-bar"></span> 
 
           <span class="icon-bar"></span> 
 
          </button> 
 
          <a class="navbar-brand" href="home">Project name</a> 
 
         </div> 
 
         <div id="navbar" class="navbar-collapse collapse"> 
 
          <ul class="nav navbar-nav"> 
 
           <li ui-sref-active="active"> 
 
            <a ui-sref="home" href="#">Home</a> 
 
           </li> 
 
           <li ui-sref-active="active" ng-show="authData"> 
 
            <a ui-sref="profile" href="#"> 
 
             My Profile 
 
            </a> 
 
           </li> 
 
          </ul> 
 
          <ul class="nav navbar-nav navbar-right"> 
 
           <li ng-hide="authData"> 
 
            <a href="#" ng-click="SignIn()"> 
 
             <span class="fa fa-facebook-official"></span> 
 
             Sign In with Facebook 
 
            </a> 
 
           </li> 
 
           <li ng-show="authData"> 
 
            <a href="#" ng-click="SignIn()"> 
 
             <span class="fa fa-sign-out"></span> 
 
             Logout 
 
            </a> 
 
           </li> 
 
          </ul> 
 
         </div> 
 
         <!--/.nav-collapse --> 
 
        </div> 
 
       </nav> 
 

 
       <div class="container"> 
 
        <div ui-view ng-show="authData"></div> 
 
        <div class="login-screen" ng-hide="authData"> 
 
         <div class="jumbotron text-center"> 
 
          <h1>Sweet login, brah.</h1> 
 
          <p class="lead">This is a pretty simple login utilizing <a href="https://angularjs.org/">AngularJS</a> and <a href="https://www.firebase.com/docs/web/libraries/angular/">AngularFire</a>.</p> 
 
          <button class="btn btn-primary btn-lg" ng-click="SignIn()"> 
 
           <span class="fa fa-facebook-official fa-fw"></span> 
 
           Sign in with Facebook 
 
          </button> 
 
         </div> 
 
        </div> 
 
       </div> 
 
      </div> 
 
     </div> 
 
     
 
     
 
     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
 
     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> 
 
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script> 
 
    
 
     <script src="https://www.gstatic.com/firebasejs/4.0.0/firebase.js"></script> 
 
      <script src="app.js"></script> 
 
     <script src="https://cdn.firebase.com/libs/angularfire/2.3.0/angularfire.min.js"></script> 
 
     <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/1.0.3/angular-ui-router.min.js"></script> 
 
    
 
    </body> 
 
    
 
    
 
</html>

相關問題