获取的WebSocket失败尝试使用匿名AngularFire验证用户时, [英] Getting Websocket Failure when trying to anonymously authenticate user using AngularFire

查看:233
本文介绍了获取的WebSocket失败尝试使用匿名AngularFire验证用户时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想验证匿名使用AngularFire用户。我想只一次验证用户(因此,如果用户已经被认证,将不会产生新的UID)。当我使用code以下,我收到了 previous_websocket_failure 的通知。无法读取空的特性UID:我在这说类型错误控制台还得到一个错误。当页面被刷新,一切工作正常。

这是我做错了什么在这里任何想法?

  app.factory('参考',['$窗口','fbURL',函数($窗口,fbURL){
  使用严格的;
  返回新的火力地堡(fbURL);
 }]);app.service('验证',['$ Q','$ firebaseAuth','参考',函数($ Q $ firebaseAuth,REF){
  VAR AUTH = $ firebaseAuth(REF);
  变种的authData = Ref.getAuth();
  的console.log(的authData);  如果(的authData){
  的console.log('已经登录与'+ authData.uid);
  }其他{
    AUTH $ authAnonymously({了rememberMe:真})。然后(函数(){
     的console.log('认证');
    })赶上(功能(错误){
      的console.log('错误');
    });
  }
}]);app.factory('项目',['$ firebaseArray','$ Q','fbURL','验证','参考',函数($ firebaseArray,$ Q,fbURL,验证,参考文献){
  变种的authData = Ref.getAuth();
  VAR REF =新的火力地堡(fbURL +'/项目/+ authData.uid);
  的console.log('authData.uid:'+ authData.uid);
  返回$ firebaseArray(REF);
}]);


解决方案

在你的项目的工厂,已承担的authData 将不能为空。有没有保证这里,因为你的项目的工厂,只要你把它注射到另一个提供商初始化。我还注意到,您的验证服务实际上并不返回任何东西。这可能意味着,调用者必须知道的内部运作,并导致相当多的耦合。更坚实的结构很可能是如下:

  app.factory('项目',函数(参考文献,$ firebaseArray){
   //返回它可以一次调用的函数
   // AUTH解决
   返回功能(UID){
      返回$ firebaseArray(Ref.child('项目')子(UID)。);
   }
});app.factory('验证',函数(参考文献,$ firebaseAuth){
   返回$ firebaseAuth(REF);
});app.controller(样本,函数($范围,验证,项目){
   如果(AUTH。$ getAuth()=== NULL){
     AUTH $ authAnonymously({了rememberMe:真})。然后(INIT)
        .catch(功能(错误){
           的console.log('错误');
        });
   }
   其他{
      (验证$ getAuth())初始化;
   }   功能的init(的authData){
      //当AUTH议决,增加项目的范围
      $ scope.projects =项目(authData.uid);
   }
});

请注意,处理在控制器和服务AUTH一般应气馁,这种处理的在路由器级是一个更优雅的解决方案。 我强烈建议投资于这种方式。查看 angularFire种子对于一些例如code。

I am trying to anonymously authenticate users using AngularFire. I want to authenticate a user only once (so, if the user has already been authenticated, a new uid won't be generated). When I use the code below, I get a previous_websocket_failure notification. I also get an error in the console that says TypeError: Cannot read property 'uid' of null. When the page is refreshed, everything works fine.

Any thoughts on what I'm doing wrong here?

app.factory('Ref', ['$window', 'fbURL', function($window, fbURL) {
  'use strict';
  return new Firebase(fbURL);
 }]);

app.service('Auth', ['$q', '$firebaseAuth', 'Ref', function ($q, $firebaseAuth, Ref) {
  var auth = $firebaseAuth(Ref);
  var authData = Ref.getAuth();
  console.log(authData);

  if (authData) {
  console.log('already logged in with ' + authData.uid);
  } else {
    auth.$authAnonymously({rememberMe: true}).then(function() {
     console.log('authenticated');
    }).catch(function(error) {
      console.log('error');
    });
  }
}]);

app.factory('Projects', ['$firebaseArray', '$q', 'fbURL', 'Auth', 'Ref', function($firebaseArray, $q, fbURL, Auth, Ref) {
  var authData = Ref.getAuth();
  var ref = new Firebase(fbURL + '/projects/' + authData.uid);
  console.log('authData.uid: ' + authData.uid);
  return $firebaseArray(ref);
}]);

解决方案

In your Projects factory, you have assumed authData will not be null. There are no guarantees here, since your Projects factory is initialized as soon as you inject it into another provider. I also noticed that your Auth service doesn't actually return anything. This probably means that the caller has to know the internal workings and leads to quite a bit of coupling. A more SOLID structure would probably be as follows:

app.factory('Projects', function(Ref, $firebaseArray) {
   // return a function which can be invoked once
   // auth is resolved
   return function(uid) {
      return $firebaseArray(Ref.child('projects').child(uid));
   }
});

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

app.controller('Example', function($scope, Auth, Projects) {
   if( Auth.$getAuth() === null ) {
     auth.$authAnonymously({rememberMe: true}).then(init)
        .catch(function(error) {
           console.log('error');
        });
   }
   else {
      init(Auth.$getAuth());
   }

   function init(authData) {
      // when auth resolves, add projects to the scope
      $scope.projects = Projects(authData.uid);
   }
});

Note that dealing with auth in your controllers and services should generally be discouraged and dealing with this at the router level is a more elegant solution. I'd highly recommend investing in this approach. Check out angularFire-seed for some example code.

这篇关于获取的WebSocket失败尝试使用匿名AngularFire验证用户时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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