尝试使用 AngularFire 匿名验证用户时出现 Websocket 失败 [英] Getting Websocket Failure when trying to anonymously authenticate user using AngularFire

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

问题描述

我正在尝试使用 AngularFire 匿名验证用户.我只想对用户进行一次身份验证(因此,如果用户已经过身份验证,则不会生成新的 uid).当我使用下面的代码时,我会收到 previous_websocket_failure 通知.我还在控制台中收到一个错误消息,内容为 TypeError: Cannot read property 'uid' of null.页面刷新后,一切正常.

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);
}]);

推荐答案

在您的项目工厂中,您假设 authData 不会为空.这里没有任何保证,因为您的 Projects 工厂在您将其注入另一个提供程序时会立即初始化.我还注意到您的 Auth 服务实际上没有返回任何内容.这可能意味着调用者必须了解内部工作原理并导致相当多的耦合.更可靠的结构可能如下所示:

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);
   }
});

请注意,通常不鼓励在您的控制器和服务中处理 auth 并处理此 在路由器级别 是一个更优雅的解决方案.我强烈建议投资这种方法.查看angularFire-seed 一些示例代码.

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.

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

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