繁体   English   中英

Ionic和Firebase用户身份验证,页面限制

[英]Ionic and Firebase User Authentication, page restriction

我构建了一个简单的应用程序,可让您使用Firebase身份验证和登录来注册帐户。

我为此使用了Ionics入门“选项卡”应用程序。 我在这里遵循了该教程: https : //www.firebase.com/docs/web/libraries/angular/guide/user-auth.html#section-routers

用于根据用户的Auth状态阻止路由。 使用下面的代码,我已阻止您仅出于测试目的而无法登录而访问仪表板选项卡,但是,您仍然可以访问它。 我一步一步地遵循了教程,这就是我所拥有的。

app.js

// Before anything else I define firebase, create a reference to the url, 
// create a service for the url and create the Auth function.

angular.module('starter', ['ionic', 'starter.controllers', 'starter.services', 'firebase'])

   .constant('FirebaseUrl', 'https://<firebase-name>.firebaseio.com/')

   .service('rootRef', ['FirebaseUrl', Firebase])

   .factory("Auth", ["$firebaseAuth", function($firebaseAuth) {
      var ref = new Firebase("https://<firebase-name>.firebaseio.com/");
      return $firebaseAuth(ref);
   }])

   ...

app.js .run()

// Then in my `.run()` function I listen for our state change that fails  
// because it needs to authorized first.

.run(function($rootScope, $state, $ionicPlatform) {

  // ionic init. code here

  $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 login page
     if(error === "AUTH_REQUIRED") {
         $state.go("login");
     }
  });
})

app.js .config()

// Finally in my `.config()` I have my $stateProvider set up and my dashboard
// tab is resolving as to have auth required to view.

.config(function($stateProvider, $urlRouterProvider) {

    $stateProvider

    // setup an abstract state for the tabs directive
    .state('tab', {
       url: '/tab',
       abstract: true,
       templateUrl: 'templates/tabs.html'
    })

    .state('login', {
       url: '/login',
       templateUrl: 'templates/login.html',
       controller: 'LoginCtrl as ctrl'
    })

    .state('tab.dash', {
       url: '/dash',
       views: {
         'tab-dash': {
            templateUrl: 'templates/tab-dash.html',
            controller: 'DashCtrl'
         }
       },
       resolve : {
         // controller will not be loaded until $waitForAuth resolves
         // Auth refers to our $firebaseAuth wrapper in the example above
         currentAuth: ["Auth", function(Auth) {
           // $waitForAuth returns a promise so the resolve waits for it to complete
           return Auth.$waitForAuth();
         }]
       }
     })

    // if none of the above states are matched, use this as the fallback
    $urlRouterProvider.otherwise('/login');

});

无论如何,当我单击登录页面(应用程序启动所在的位置)上的临时链接时,即使没有登录,它仍会带我进入仪表板页面!

我在这里想念什么?

在您发布的示例中,其显示为在Firebase网站上发布的示例中,显示为return Auth.$requireAuth(); 并且您已经return Auth.$waitForAuth();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM