我正在尝试将新的Firebase与Auth系统一起使用,并通过$routeProvider
限制我的resolves
中的路由。
但是,我不太了解。
这就是我所拥有的。在我的.config函数中,我正在定义路由并初始化firebase。以下是我的配置块中。
$routeProvider
.when('/', {
templateUrl: 'templates/dashboard.ejs',
//resolve. this needs restricted
})
.when('/login', {
templateUrl: 'templates/login.ejs'
})
firebase.initializeApp(config);
我在新的文档站点上发现这些功能可用,这些功能在
.run()
块中列出了。.run(function($rootScope, $location) {
$rootScope.authentication = firebase.auth();
/*firebase.auth().onAuthStateChanged(function(user) {
if(user) {
$rootScope.user = user;
} else {
$rootScope.user = false;
$location.path('/login')
}
})*/
var user = firebase.auth().currentUser;
if (user) {
$rootScope.user = user;
} else {
$rootScope.user = false;
console.log('No User!');
$location.path('/login');
}
})
现在,我上面的内容仅触发了
every other time
,而我访问了我网站上的任何网址。所以我的问题是,如何将.run()函数中的内容用作我的routeProvider的解析,以便能够再次限制路由。
使用旧的firebase,您只需像下面那样调用$ firebaseAuth()并具有一个如下所示的routeChangeError函数。
// In config block. **old way**
$routeProvider
.when('/', {
templateUrl: 'templates/dashboard.ejs',
resolve: {
"currentAuth": ["$firebaseAuth", function($firebaseAuth) {
var ref = new Firebase(fbUrl);
var authObj = $firebaseAuth(ref);
return authObj.$requireAuth();
}]
}
})
然后是.run()块中的routechangeerror。
// .run block **old way**
.run(function($rootScope, $location) {
$rootScope.$on("$routeChangeError", function(event, current, previous, eventObj) {
if (eventObj === 'AUTH_REQUIRED') {
console.log('auth required!');
$location.path("/login");
}
});
})
这不再起作用,因为您不再使用
$firebaseAuth()
。或new Firebase(fbUrl);
都可以。更新
我觉得这有点困惑,但是再仔细研究一下路线,我想到了这一点。
在我的路线上解析:
.when('/', {
templateUrl: 'templates/dashboard.ejs',
resolve: {
userAuthenticated: ["$http", "$q", function($http, $q) {
var deferred = $q;
if(firebase.auth().currentUser) {
deferred.resolve();
} else {
deferred.reject();
console.log('Really!?');
}
return deferred.promise;
}]
}
})
然后是一个简单的routeChangeError。
$rootScope.$on("$routeChangeError", function (event, current, previous, rejection) {
alert("Not authorised");
})
唯一的问题是,似乎deferred.promise返回undefined。它没有激活routeChangeError函数。即使我的
console.log()
被调用。可能是因为没有用户通过身份验证时
firebase.auth().currentUser
返回null吗? 最佳答案
这些方法已重命名为$waitForSignIn()
和$requireSignIn
。
您还可以使用$firebaseRefProvider
服务来配置数据库URL,以便它自动配置$firebaseAuthService
。
angular.module('app', ['firebase'])
.constant('FirebaseDatabaseUrl', '<my-firebase-url>')
.controller('HomeCtrl', HomeController)
.config(function($firebaseRefProvider, FirebaseDatabaseUrl, $routeProvider) {
$firebaseRefProvider.registerUrl(FirebaseDatabaseUrl);
$routeProvider.when("/home", {
controller: "HomeCtrl",
templateUrl: "views/home.html",
resolve: {
// controller will not be loaded until $waitForSignIn resolves
"firebaseUser": function($firebaseAuthService) {
return $firebaseAuthService.$waitForSignIn();
}
}
}).when("/account", {
controller: "AccountCtrl",
templateUrl: "views/account.html",
resolve: {
// controller will not be loaded until $requireSignIn resolves
"firebaseUser": function(Auth) {
// If the promise is rejected, it will throw a $stateChangeError
return $firebaseAuthService.$requireSignIn();
}
}
});
});
function HomeController($scope, $firebaseRef, firebaseUser) {
}