我正在开发一个Angular网络应用,该应用使用Firebase在用户登录或注册时对其进行身份验证。我想限制转到/ dashboard网址,除非他们已登录。我尝试关注Firebase的文档,但我想出现错误。
我认为我遇到问题的地方是让我的控制器代码与提供的代码一起工作。我一直收到错误“Unknown provider: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 :(得分:0)
我可以看到至少在问题上可能存在的问题。在您的路由器中,您可以参考“Auth”,但看不到该服务/工厂的代码,因此您可能没有设置该代码。请注意,“Auth”是一个自定义工厂。
你不需要在代码中的某处:
app.factory("Auth", ["$firebaseAuth",
function($firebaseAuth) {
var ref = new Firebase("https://docs-sandbox.firebaseio.com", "example3");
return $firebaseAuth(ref);
}
])
另外,您能否提供您收到的错误?
韦恩