0

之前檢查Firebase身份驗證我想要一個路徑來檢查並檢查Firebase用戶是否在將某人重定向到/map.html頁面之前登錄。如何在routeProvider發送至頁面

.when('/map', { 
      templateUrl: 'views/map.html', 
      controller: 'MapCtrl', 
      resolve: { 
       loggedIn: onlyLoggedIn() 
      } 

     }); 

我一直在使用決心嘗試(見上面的代碼),但我只得到從onlyLoggedIn()函數,它說:「用戶不定義」的錯誤。用戶定義如果我嘗試在用戶功能來傳遞我得到一個錯誤「未知提供商:用戶」

var onlyLoggedIn = function ($location,$q,user) { 
    var deferred = $q.defer(); 
    console.log("well: "+user); 
    if (user) { 
     console.log("user is signed in "); 
     deferred.resolve(); 
    } else { 
     deferred.reject(); 
     $location.url('/signup'); 
    } 
    return deferred.promise; 
}; 

我應該如何去了解他們路由到之前,如果用戶使用火力簽署檢查/地圖頁面?

火力地堡給我的用戶變量,並在我的應用程序它的工作原理「其他」的地方,所以這個問題是不是用戶沒有被定義的,但我的onlyLoggedIn功能不是「讓」它(?)

+0

您使用的是哪個版本的FIrebase SDK?你正在使用哪個版本的AngularFIre? – georgeawg

回答

0

安全Firebase中的規則可以防止未經授權查看數據頁面。您需要客戶端解決方案來重定向頁面。你在路線上做。 這是Angular Firebase的完整代碼

這是簡單的代碼演示。我希望這會爲你工作。

angular.module('myApp.routes', ['ngRoute', 'simpleLogin']) 

    .constant('ROUTES', { 
    '/home': { 
     templateUrl: 'partials/home.html', 
     controller: 'HomeCtrl', 
     resolve: { 
     // forces the page to wait for this promise to resolve before controller is loaded 
     // the controller can then inject `user` as a dependency. This could also be done 
     // in the controller, but this makes things cleaner (controller doesn't need to worry 
     // about auth status or timing of displaying its UI components) 
     user: ['simpleLogin', function(simpleLogin) { 
      return simpleLogin.getUser(); 
     }] 
     } 
    }, 
    '/chat': { 
     templateUrl: 'partials/chat.html', 
     controller: 'ChatCtrl' 
    }, 
    '/login': { 
     templateUrl: 'partials/login.html', 
     controller: 'LoginCtrl' 
    }, 
    '/account': { 
     templateUrl: 'partials/account.html', 
     controller: 'AccountCtrl', 
     // require user to be logged in to view this route 
     // the whenAuthenticated method below will resolve the current user 
     // before this controller loads and redirect if necessary 
     authRequired: true 
    } 
    }) 

    /** 
    * Adds a special `whenAuthenticated` method onto $routeProvider. This special method, 
    * when called, invokes the requireUser() service (see simpleLogin.js). 
    * 
    * The promise either resolves to the authenticated user object and makes it available to 
    * dependency injection (see AuthCtrl), or rejects the promise if user is not logged in, 
    * forcing a redirect to the /login page 
    */ 
    .config(['$routeProvider', function($routeProvider) { 
    // credits for this idea: https://groups.google.com/forum/#!msg/angular/dPr9BpIZID0/MgWVluo_Tg8J 
    // unfortunately, a decorator cannot be use here because they are not applied until after 
    // the .config calls resolve, so they can't be used during route configuration, so we have 
    // to hack it directly onto the $routeProvider object 
    $routeProvider.whenAuthenticated = function(path, route) { 
     route.resolve = route.resolve || {}; 
     route.resolve.user = ['requireUser', function(requireUser) { 
     return requireUser(); 
     }]; 
     $routeProvider.when(path, route); 
    } 
    }]) 

    // configure views; the authRequired parameter is used for specifying pages 
    // which should only be available while logged in 
    .config(['$routeProvider', 'ROUTES', function($routeProvider, ROUTES) { 
    angular.forEach(ROUTES, function(route, path) { 
     if(route.authRequired) { 
     // adds a {resolve: user: {...}} promise which is rejected if 
     // the user is not authenticated or fulfills with the user object 
     // on success (the user object is then available to dependency injection) 
     $routeProvider.whenAuthenticated(path, route); 
     } 
     else { 
     // all other routes are added normally 
     $routeProvider.when(path, route); 
     } 
    }); 
    // routes which are not in our map are redirected to /home 
    $routeProvider.otherwise({redirectTo: '/home'}); 
    }]) 

    /** 
    * Apply some route security. Any route's resolve method can reject the promise with 
    * { authRequired: true } to force a redirect. This method enforces that and also watches 
    * for changes in auth status which might require us to navigate away from a path 
    * that we can no longer view. 
    */ 
    .run(['$rootScope', '$location', 'simpleLogin', 'ROUTES', 'loginRedirectPath', 
    function($rootScope, $location, simpleLogin, ROUTES, loginRedirectPath) { 
     // watch for login status changes and redirect if appropriate 
     simpleLogin.watch(check, $rootScope); 

     // some of our routes may reject resolve promises with the special {authRequired: true} error 
     // this redirects to the login page whenever that is encountered 
     $rootScope.$on("$routeChangeError", function(e, next, prev, err) { 
     if(angular.isObject(err) && err.authRequired) { 
      $location.path(loginRedirectPath); 
     } 
     }); 

     function check(user) { 
     if(!user && authRequired($location.path())) { 
      $location.path(loginRedirectPath); 
     } 
     } 

     function authRequired(path) { 
     return ROUTES.hasOwnProperty(path) && ROUTES[path].authRequired; 
     } 
    } 
    ]); 
+0

感謝您的信息,但simpleLogin.js不在您提供的鏈接中。 – hunterInt