Javascript Angularfire ngRoute/解决问题,跳转至其他
我正在使用最新的Angular+Firebase并尝试设置登录授权系统。我有home.html,它包含login+signup链接,转到login.html并添加凭据效果很好(提交时记录正确的UID),但它应该路由到dash.html,但返回home.html 我发现这似乎是我的resolve函数的问题,因为当我删除时问题就消失了。否则。但我想我还是想要(需要?)它 如果我已登录(但重定向到home.html),我仍然可以通过URL访问dash.html,如果我使用dash.html的注销功能,我将无法再次访问它,这就是应该的方式。 但是我不明白为什么我会被重定向到home.html 以下是一些代码,欢迎提供帮助: My.run、.config和routesJavascript Angularfire ngRoute/解决问题,跳转至其他,javascript,angularjs,firebase,angular-routing,ngroute,Javascript,Angularjs,Firebase,Angular Routing,Ngroute,我正在使用最新的Angular+Firebase并尝试设置登录授权系统。我有home.html,它包含login+signup链接,转到login.html并添加凭据效果很好(提交时记录正确的UID),但它应该路由到dash.html,但返回home.html 我发现这似乎是我的resolve函数的问题,因为当我删除时问题就消失了。否则。但我想我还是想要(需要?)它 如果我已登录(但重定向到home.html),我仍然可以通过URL访问dash.html,如果我使用dash.html的注销功能,
app.run(['$rootScope', '$location',
function($rootScope, $location){
$rootScope.$on('$routeChangeError',
function(event, next, previous, error){
if(error === 'AUTH_REQUIRED'){
$location.path('/home');
}
});
}]);
app.config(['$routeProvider', '$locationProvider', function($routeProvider,
$locationProvider){
$routeProvider
.when('/home', {
templateUrl: '/home.html',
controller: 'homeController'
})
.when('/login', {
templateUrl: '/login.html',
controller: 'loginController',
resolve: {
'currentAuth': ['Auth', function(Auth){
return Auth.$waitForAuth();
}]
}
})
.when('/dash', {
templateUrl: '/dash.html',
controller: 'dashController',
resolve: {
'currentAuth': ['Auth', function(Auth){
return Auth.$requireAuth();
}]
}
})
.otherwise({ redirectTo: '/home' });
}]);
我的登录控制器:
app.controller('loginController', ['currentAuth', '$scope', '$firebaseAuth',
'Auth', '$location', '$rootScope',
function(currentAuth, $scope, $firebaseAuth, Auth, $location, $rootScope){
var ref = new Firebase('https://url.firebaseio.com');
$scope.auth = $firebaseAuth(ref);
$scope.loginUser = function(){
$scope.auth = Auth;
$scope.auth.$authWithPassword({
email:$scope.email,
password:$scope.password
}, {
remember: 'sessionOnly'
}).then(function(authData) {
console.log('Logged in as: ', authData.uid);
$scope.auth.$onAuth(function(authData) {
$rootScope.auth = true;
$scope.auth = Auth;
$location.path('/dash.html');
})
}).catch(function(error) {
console.log('There was an error: ', error);
});
};
}]);
以及我的工厂和模块:
var app=angular.module('app',['firebase','ngRoute'])
这与你的决心有关 如果你看一下文档 您将看到他们使用
$waitForSignIn
或
功能。我知道这一点,因为我也做过同样的事情。尝试一下,它应该可以工作不熟悉Firebase,但我觉得奇怪的是,服务和控制器都使用相同的URL创建了一个新的Firebase实例。认为控制器内的实例会有新的状态,而服务内的实例不会,但它是路由中使用的实例。@GillesC:也许,我也不太喜欢这个,但我不认为这是问题所在,因为很明显,我可以在授权时访问dash(没有身份验证=没有访问正在工作)。当我登录时,我可以在重定向到/home之前很快看到URL中的/dash。或者可能是“需要验证”不起作用?我觉得currentAuth的解决方案中有些东西并没有完全解决。。。
$waitForSignIn
$requireSignIn