解决我试图使用新的火力地堡与验证系统,并通过resolves
限制在我$routeProvider
路线。AngularJS路线,并与新的火力地堡验证
但是,我不是很理解。
这就是我所拥有的。在我的.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
我访问我的网站上的任何URL。
所以我的问题是,我该如何采取什么在我的.RUN()函数,并使其成为我routeProvider一个决心,所以我能够再次限制路线。
随着老火力,你只需调用$ 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);
。
UPDATE
我觉得这是一个有点混乱,但寻找到的路线多一点,我想出了这个。
在我的路线的决心:
.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
返回空?
我不知道您使用的是什么版本或第三方依赖项,但这对我来说完全不起作用,也不像我在文档中找到的任何东西。当我尝试将firebase模块加载到我的应用程序中时,出现一个未定义的错误。那么,你在使用什么第三方? –
您也不会像这样定义您的网址,您可以使用Firebase提供的代码块初始化Firebase。我的问题不是关于第三方依赖关系。 –
我在Firebase团队工作,我是AngularFire的维护者。我写了'$ firebaseRefProvider'和'$ firebaseAuthService'功能。这是一种使用数据库引用处理依赖注入的方法。我们正在为它的文档工作。查看github上的文档以获取身份验证方法:https://github.com/firebase/angularfire/blob/master/docs/guide/user-auth.md。 –