2015-09-04 171 views
1

我正在使用Firebase在用户登录或注册时对其进行身份验证的Angular Web应用程序。我想限制去/仪表板的网址,除非他们已经登录。我试着跟随Firebase的文档,但我提出了错误。AngularFire(Firebase)和路由器身份验证问题

我想我遇到问题的地方是让我的控制器代码与所提供的代码一起工作。我不断收到错误“未知的提供者:AuthProvider < - Auth < - currentAuth”,所以我现在就拿出他们的控制器代码。

任何帮助将是伟大的!

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

而且我的代码:

ROUTER CONFIG

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); 
    }; 

}); 

回答

0

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

你不需要这些地方在你的代码:

app.factory("Auth", ["$firebaseAuth", 


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

此外,您还可以提供您收到的错误?

Wayne