2015-08-18 20 views
0

Мне сложно работать с requireAuth. Я пробовал как ngroute, так и uirouter, и если пользователь не заходит в систему, я просто хочу перенаправить его обратно на домашнюю страницу.Firebase requireAuth не работает

В моем приложении у меня есть 1 страница с несколькими контроллерами, где я хочу установить вышеуказанное правило.

Это завод и запустить метод:

app.factory("AuthFactory", ["$firebaseAuth", function($firebaseAuth) { 
    var ref = new Firebase("https://torrid-heat-237.firebaseio.com"); 
    return $firebaseAuth(ref); 
    } 
]); 

// for ui-router 
app.run(["$rootScope", "$state", function($rootScope, $state) { 
$rootScope.$on("$stateChangeError", function(event, toState, toParams, fromState, fromParams, error) { 
    // We can catch the error thrown when the $requireAuth promise is rejected 
    // and redirect the user back to the home page 
    if (error === "AUTH_REQUIRED") { 
    $state.go("/"); 
    } 
}); 
}]); 

, я написал .state метод:

app.config(["$stateProvider", function ($stateProvider) { 
$stateProvider 
    .state('geniuses', { 
     url: '/geniuses', 
     abstract: true, 
     controller: 'GetAllGeniuses', 
     templateUrl: "views/listAllgeniuses", 
     resolve: { 
     "currentAuth": ["AuthFactory", function(AuthFactory) { 
      return AuthFactory.$requireAuth(); 
     }] 
     } 
    }).state('geniuses', { 
     url: '/geniuses', 
     abstract: true, 
     controller: 'SearchAGenius', 
     templateUrl: "views/listAllgeniuses", 
     resolve: { 
     "currentAuth": ["AuthFactory", function(AuthFactory) { 
      return AuthFactory.$requireAuth(); 
     }] 
     } 
    }) 
}]); 

И, наконец, в обоих контроллерах, я жду AuthFactory .requireAuth для решения. Но, что происходит, когда я ударяю URL-адрес без регистрации, он остается там сам, и даже когда я вхожу в систему, он отображает одну и ту же страницу.

Что именно я сделал неправильно здесь ?

ответ

0

У меня была такая же проблема

И, наконец, можно решить проблему.

Убедитесь, что вы вызываете $ unauth() при выходе из системы. Первый раз, когда вы вошли в систему, все будет хорошо. Реальная проблема возникает, когда вы закрываете сессию без $ unauth().

Так что, если у вас есть функция, чтобы отключала использовать это:

AuthFactory.$unauth(); 

Это пример из моего кода:

app.factory('Auth', ["$firebaseAuth","settings", 
function($firebaseAuth,settings){ 

return { 
    oauth: function() { 

     var ref = new Firebase(settings.firebaseBaseUrl); 

     return $firebaseAuth(ref); 
    } 
}]); 



.state("votes",{ 
    url:"/restaurant/poll", 
    views:{ 
    "main":{ 
     templateUrl:"views/restaurant/poll/main.html", 
     controller: "pollsController" 
    } 
    }, 
    data: {pageTitle:'Encuesta'}, 
    resolve: { 
    "currentAuth": ["Auth", function(Auth) { 
     return Auth.oauth().$requireAuth(); 
    }] 
    } 
}); 


app.run(function($rootScope,settings, $state, $location, $localStorage, Auth) { 

    $rootScope.$on("$stateChangeError", function(event, toState, toParams, fromState, fromParams, error) { 
    if (error === "AUTH_REQUIRED") { 
     $state.go('home'); 
    } 
}); 

И наконец:

MetronicApp.controller('HeaderController', ['$scope', "$rootScope", "Auth", "$state", "$localStorage", "toaster", 
function($scope, $rootScope, Auth, $state, $localStorage) { 


    $rootScope.logout = function(to){ 
    to = to == undefined ? 'home' : to; 
    Auth.oauth().$unauth(); 
    $state.go(to); 
    }; 


}]); 
Смежные вопросы