Auth重定向到错误的路径

时间:2016-04-27 12:55:43

标签: javascript angularjs firebase angularfire angular-routing

我的AngularFire Auth功能有这种奇怪的行为。 用户使用电子邮件+密码登录,成功后应重定向到/ dash,但重定向到/ home。 / dash被锁定为unauth并按预期工作,但是当我登录并被重定向到/ home时,如果我在浏览器中键入/破折号它是可访问的。所以,它正在工作,但我的重定向行为显然是不稳定的。此外,它似乎正在尝试访问/破解,因为它在成功登录后将我发送到/ home之前很快就会闪烁。

我使用的是最新的Angular + AngularFire + Firebase。 我试图设置一个plunkr-view,但它看起来像是plunkr和firebase不起作用。

无论如何,这是我的授权代码和我的路线代码,欢迎所有想法!

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

// Login user, if true redirect to dash.html, if false, don't redirect, show error message
$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');
  })
  // If error with login, catch it and prompt it
  }).catch(function(error) {
    $('#signBlock').text(error);
  });
 };
}
]);

路线:

app.run(['$rootScope', '$location', 'Auth',
function($rootScope, $location, Auth){
$rootScope.$on('$routeChangeError',
function(event, next, previous, error){
  if(error === 'AUTH_REQUIRED'){
    console.log('AUTH REQ!')
    $location.path('/home');
  }
});
}]);


// Config for routing
app.config(['$routeProvider', '$locationProvider',    function($routeProvider, $locationProvider){
$routeProvider

// Route for landing page
.when('/home', {
 templateUrl: '/home.html',
 controller: 'homeController'
})

// Route for login page
.when('/login', {
 templateUrl: '/login.html',
 controller: 'loginController',
 resolve: {
  'currentAuth': ['Auth', function(Auth){
    return Auth.$waitForAuth();
  }]
}
})

// Route for user dashboard
.when('/dash', {
 templateUrl: '/dash.html',
 controller: 'dashController',
 resolve: {
  'currentAuth': ['Auth', function(Auth){
    return Auth.$requireAuth();
  }]
 }
 })

 .otherwise({ redirectTo: '/home' });

}]);

0 个答案:

没有答案