所以我下面thinksters梦幻足球应用教程,但每当我注销,并尝试创建一个新帐户。新帐户被创建并取代所有关于火力老用户与新用户信息的信息。
我猜这是在那里没有得到关于注销清除一个范围的问题吗?
使用严格的;/* 服务 */angular.module('fantasyApp.services.login',['fantasyApp.services.profileCreator']) .factory('login服务',['angularFireAuth','profileCreator','$的位置,$ rootScope', 功能(angularFireAuth,profileCreator,$位置$ rootScope){ 返回{ 登录:功能(电子邮件,传递,重定向回调){ VAR P = angularFireAuth.login('密码',{ 电子邮件:电子邮件, 密码:通过, 与rememberMe:真 }); p.then(功能(用户){ 如果(重定向){ $ location.path(重定向); } 回调和放大器;&安培;回调(NULL,用户); }, 回电话); }, 注销:功能(redirectPath){ angularFireAuth.logout(); 如果(redirectPath){ $ location.path(redirectPath); $ scope.destroy() } }, 的createAccount:功能(姓名,电子邮件,移动,传球,回调){ angularFireAuth._authClient.createUser(电子邮件,传中,函数(ERR,用户){ 如果(回调){ 回调(ERR,用户); $ rootScope $适用()。 } }); }, createProfile:profileCreator }}])
解决方案
这个问题涉及到在文件应用此位$ C $的C / JS /控制器/ headercontroller.js。
$ $范围在('angularFireAuth:登录',函数(){ angularFire(新火力地堡(FBURL +'/用户/+ $ scope.auth.id),$范围,用户);});
这里发生的是,每次 angularFireAuth:登录
获取的$ broadcast'd,我们挂接 $ scope.user
到位于 FBURL +'/用户/+ $ scope.auth.id 。现在,问题是,如果您注销,然后重新登录, angularFireAuth:登录
获得$ broadcast'd两次。这不会是除了那些我们在回调旋转了一个angularFire事实的问题。这样做的结果是,我们现在有两个 angularFire
取值绑 $ scope.user
。这意味着,如果 $ scope.user
得到改变,我们会写信给两个独立的火力地堡参考的。
如果附上一份 angularFire
到 $ scope.user
,例如 $ scope.user
被设置为 {名:泰勒}
,那么当我们注销并重新登录,当前值 $ scope.user
被写入新的 angularFire
,这是绑到不同用户的路径。这将导致这个奇怪的账户覆盖的行为。
总之,解决办法是使用解除关联()
方法时的 angularFire
承诺解决过去了。
例如,试试这个。
$ $范围在('angularFireAuth:登录',函数(){ 如果($ scope.disassociateUserData){ $ scope.disassociateUserData(); } angularFire(新火力地堡(FBURL +'/用户/+ $ scope.auth.id),$范围,用户),然后(功能(取消关联){ $ scope.disassociateUserData =撇清; };});
So I am following thinksters fantasy football app tutorial but whenever I logout and try creating a new account. The new account gets created and replaces all the information about the old user on firebase with the new user info.
I'm guessing this is a scope issue where it is not getting cleared on logout?
'use strict';
/* Services */
angular.module('fantasyApp.services.login', ['fantasyApp.services.profileCreator'])
.factory('loginService', ['angularFireAuth', 'profileCreator', '$location', '$rootScope',
function(angularFireAuth, profileCreator, $location, $rootScope) {
return {
login: function(email, pass, redirect, callback) {
var p = angularFireAuth.login('password', {
email: email,
password: pass,
rememberMe: true
});
p.then(function(user) {
if( redirect ) {
$location.path(redirect);
}
callback && callback(null, user);
}, callback);
},
logout: function(redirectPath) {
angularFireAuth.logout();
if(redirectPath) {
$location.path(redirectPath);
$scope.destroy()
}
},
createAccount: function(name, email, mobile, pass, callback) {
angularFireAuth._authClient.createUser(email, pass, function(err, user) {
if(callback) {
callback(err, user);
$rootScope.$apply();
}
});
},
createProfile: profileCreator
}
}])
解决方案
This issue relates to this bit of code in the file app/js/controllers/headercontroller.js.
$scope.$on('angularFireAuth:login', function() {
angularFire(new Firebase(FBURL+'/users/'+$scope.auth.id), $scope, 'user');
});
What happens here is that every time angularFireAuth:login
get’s $broadcast‘d, we’re hooking up $scope.user
to a Firebase ref located at FBURL+'/users/'+$scope.auth.id
. Now, the issue is that if you log out, then log back in, angularFireAuth:login
gets $broadcast‘d twice. This wouldn’t be an issue except for the fact that we’re spinning up an angularFire in the callback. The result of this is that we now have two angularFire
s tied to $scope.user
. This means, that if $scope.user
gets changed, we’ll write to two separate Firebase ref’s.
If we attach an angularFire
to $scope.user
and, for example $scope.user
gets set to {name: "Tyler"}
, then when we log out and log back in, the current value of $scope.user
gets written to the new angularFire
, which is tied to a different user's path. This results in this weird account overwriting behavior.
Anyway, the solution is to use the disassociate()
method passed when angularFire
’s promise is resolved.
For example, try this.
$scope.$on('angularFireAuth:login', function() {
if ($scope.disassociateUserData) {
$scope.disassociateUserData();
}
angularFire(new Firebase(FBURL+'/users/'+$scope.auth.id), $scope, 'user').then(function (disassociate) {
$scope.disassociateUserData = disassociate;
};
});