【问题标题】:Trouble with AngularFire (Firebase) and Authenticating with RoutersAngularFire(Firebase)和路由器身份验证的问题
【发布时间】:2015-11-30 20:17:08
【问题描述】:

我正在开发一个 Angular Web 应用程序,该应用程序使用 Firebase 在用户登录或注册时对其进行身份验证。我想限制访问 /dashboard 网址,除非他们已登录。我尝试关注 Firebase 的文档,但我遇到了错误。

我认为我遇到的问题是让我的控制器代码与提供的代码一起工作。我一直收到错误“未知提供者:AuthProvider

任何帮助都会很棒!

这是文档链接:https://www.firebase.com/docs/web/libraries/angular/guide/user-auth.html#section-routers

还有我的代码:

路由器配置

var app = angular.module('maggsLashes', ['ngRoute', 'ui.calendar', 'firebase']);

app.config(function($routeProvider) {
  $routeProvider
     .when('/dashboard', {
      templateUrl: 'app/templates/dashboardTmpl.html',
      controller: 'dashboardCtrl',
      resolve: {
        // controller will not be loaded until $requireAuth resolves
        // Auth refers to our $firebaseAuth wrapper in the example above
        "currentAuth": ["Auth", function(Auth) {
          // $requireAuth returns a promise so the resolve waits for it to complete
          // If the promise is rejected, it will throw a $stateChangeError (see above)
          return Auth.$requireAuth();
        }]
      }
    })

仪表板控制器

app.controller('dashboardCtrl', function($scope, $firebaseAuth, $location) {

  var ref = new Firebase("https://maggslashes.firebaseio.com/");

  var auth = $firebaseAuth(ref);

//   app.controller("AccountCtrl", ["currentAuth", function(currentAuth) {
//   // currentAuth (provided by resolve) will contain the
//   // authenticated user or null if not logged in
// }]);

  // console.log(auth);
  console.log("Matt is pretty much awesome");

  ref.onAuth(function(authData) {
    if (authData) {
      console.log("User is authenticated w uid:", authData);

    }
    else {
      console.log("client sucks");
    }
  });

  $scope.logOut = function() {
    $location.path('/');
    $scope.apply();
    ref.unauth();
    console.log(authData.uid);
  };

});

【问题讨论】:

    标签: angularjs authentication firebase ngroute


    【解决方案1】:

    我可以看到至少有什么问题。在您的路由器中,您指的是“Auth”,但看不到该服务/工厂的代码,因此您可能没有设置。请注意,“Auth”是自定义工厂。

    你的代码中不需要这个:

    app.factory("Auth", ["$firebaseAuth",
    
    
    function($firebaseAuth) {
        var ref = new Firebase("https://docs-sandbox.firebaseio.com", "example3");
        return $firebaseAuth(ref);
      }
    ])
    

    另外,你能提供你收到的错误吗?

    韦恩

    【讨论】:

      猜你喜欢
      • 2017-09-24
      • 1970-01-01
      • 2018-02-04
      • 2015-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-26
      • 2015-03-19
      相关资源
      最近更新 更多