Firebase身份验证问题$ requireAuth() [英] Issues with Firebase Auth.$requireAuth()

查看:130
本文介绍了Firebase身份验证问题$ requireAuth()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在阅读Firebase的用户身份验证部分,以找到一种方法来阻止未经身份验证的用户访问特定页面(即未登录时)。



我遇到的解决方案是使用snipet的代码:

  resolve:{
/ /控制器将不被加载,直到$ requireAuth解析
// Auth指向上面例子中的$ firebaseAuth包装器
'currentAuth':['Auth',function(Auth){
/ / $ requireAuth返回一个承诺,以便解析等待它完成
//如果承诺被拒绝,它将抛出一个$ stateChangeError(见上面)
return Auth。$ requireAuth();






$ b $ p $所以当我把它放入并尝试访问没有被登录的具体页面,我在控制台中得到了以下错误消息:

$ $ p $ code错误:[$ injector:unpr]未知提供者:AuthProvider< - Auth< - currentAuth

如果我尝试登录后,我无法导航到该特定页面。

我一直在寻找几个小时来找到解决方案,大多数人说这是一个依赖性问题没有ngRoute包含在我的app.js文件中。然而,我确实有这种依赖关系,所以我知道这不是问题。
$ b $ $ p $角$ b $ .module('应用程序' ,[
'ngAnimate',
'ngCookies',
'ngResource',
'ngRoute',
'ngSanitize',
'ngTouch',
'ui.sortable',
'firebase',
'angular-toArrayFilter'
])
.constant('fb',{
url :'https://< url> .firebaseio.com /'
})
.factory('fireBaseRef',function(fb){
return new Firebase(fb.url) ;
.factory('UserAuth',function($ firebaseAuth,fireBaseRef){
return $ firebaseAuth(fireBaseRef);
})
.config(函数($ routeProvider){
$ routeProvider
.when('/',{
templateUrl:'views / login.html',
controller:'LoginCtrl',
controllerAs:'login_controller'
})
.when('/ view_resources' ,{
templateUrl:'views / view_resources.html',
controller:'ViewResourcesCtrl',
controllerAs:'view_resources',
resolve:{
// controller直到$ requireAuth解析
// Auth指向我们在上面例子中的$ firebaseAuth包装
'currentAuth':['Auth',function(Auth){
// $ requireAuth返回一个promise,等待它完成
//如果promise被拒绝,它将抛出一个$ stateChangeError(见上)
return Auth。$ requireAuth();
}]
}
})
.otherwise({
redirectTo:'/'
});
});

感谢您的宝贵时间。

解决方案

我认为你的工厂

  .factory('UserAuth',function($ firebaseAuth,fireBaseRef ){
return $ firebaseAuth(fireBaseRef);
})

应该是命名为Auth,如下所示:

  .factory('Auth',function($ firebaseAuth,fireBaseRef){
$ return $ firebaseAuth(fireBaseRef);
})


So I was reading up on Firebase's "User Authentication" section to find a way to stop un-authenticated users from accessing specific pages (i.e. when they're not logged in)

The solution I came across, was to use this snipet of code:

resolve: {
      // controller will not be loaded until $requireAuth resolves
      // Auth refers to our $firebaseAuth wrapper in the example above
      'currentAuth': ['Auth', function(Auth) {
        // $requireAuth returns a promise so the resolve waits for it to complete
        // If the promise is rejected, it will throw a $stateChangeError (see above)
        return Auth.$requireAuth();
      }]
    }

So after I put that in and tried to access the specific page without being logged in, I got the following error message in the console:

Error: [$injector:unpr] Unknown provider: AuthProvider <- Auth <- currentAuth

If I try to log in after words, I am unable to navigate to that specific page.

I've been searching around for a few hours to find a solution to this and most people said it was a dependency issue with not having "ngRoute" included in my app.js file. However I do have that dependency included so I know that's not the issue.

angular
  .module('App', [
    'ngAnimate',
    'ngCookies',
    'ngResource',
    'ngRoute',
    'ngSanitize',
    'ngTouch',
    'ui.sortable',
    'firebase',
    'angular-toArrayFilter'
  ])
  .constant('fb', {
    url: 'https://<url>.firebaseio.com/'
  })
  .factory('fireBaseRef', function(fb) {
    return new Firebase(fb.url);
  })
  .factory('UserAuth', function($firebaseAuth, fireBaseRef){
    return $firebaseAuth(fireBaseRef);
  })
  .config(function ($routeProvider) {
    $routeProvider
      .when('/', {
        templateUrl: 'views/login.html',
        controller: 'LoginCtrl',
        controllerAs: 'login_controller'
      })
      .when('/view_resources', {
        templateUrl: 'views/view_resources.html',
        controller: 'ViewResourcesCtrl',
        controllerAs: 'view_resources',
        resolve: {
          // controller will not be loaded until $requireAuth resolves
          // Auth refers to our $firebaseAuth wrapper in the example above
          'currentAuth': ['Auth', function(Auth) {
            // $requireAuth returns a promise so the resolve waits for it to complete
            // If the promise is rejected, it will throw a $stateChangeError (see above)
            return Auth.$requireAuth();
          }]
        }
      })
      .otherwise({
        redirectTo: '/'
      });
  });

Thanks for your time.

解决方案

I think your factory

.factory('UserAuth', function($firebaseAuth, fireBaseRef){
    return $firebaseAuth(fireBaseRef);
  })

should be named as "Auth" as follow:

.factory('Auth', function($firebaseAuth, fireBaseRef){
    return $firebaseAuth(fireBaseRef);
  })

这篇关于Firebase身份验证问题$ requireAuth()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆