Firebase使用户对象来自验证数据 [英] Firebase make user object from auth data
问题描述
$ firebaseArray
这工作正常,但我不知道如何抓取当前用户登录后更新,破坏或对该用户数据做任何事情。我已经做了循环的用户数组,并匹配从用户数组项目和设置为在用户数组相同的 auth.uid
项目的uid对象创建。如果有大量的用户数组,并且需要在多个页面上完成,这似乎真的是无效循环。 我目前的尝试是使用不同的方法,如:
$ $ p $
angular.module('haulya.main')
.controller('RegisterController',['Auth', '$ scope','User','$ ionicPlatform','$ cordovaCamera','CurrentUserService',
函数(Auth,$ scope,User,$ ionicPlatform,$ cordovaCamera,CurrentUserService){
//控制器的作用域变量
$ scope.user = {};
console.log(User);
$ scope.createUser = function(isValid){
var userModel;
$ scope.submitted = true;
//成功或失败用户创建的消息
$ scope.user.message = null;
$ scope.user.error = null;
//如果表单填写有效
if(isValid){
//使用电子邮件和密码创建用户firebase身份验证方法
A ($ {
email:$ scope.user.email,
password:$ scope.user.password
})
.then(function(userData){
userModel = {
uid:userData.uid,
photo:$ scope.user.photo || null,
firstName:$ scope.user.firstName,
lastName:$ scope.user.lastName,
email:$ scope.user.email,$ b $ cell:$ scope。 user.cell,
dob:$ scope.user.dob.toString(),
city:$ scope.user.city,
state:$ scope.user.state,
zip:$ scope.user.zip
}
//将新用户添加到配置文件数组$ b $ User.create(userModel).then(function(user){
$ scope.sharedUser = User.get(user.path.o [1]);
});
$ scope.user.message =为邮件创建的用户:+ $ scope.user.email;
)
.catch(函数(错误){
//设置错误消息的上下文
if(error.code =='INVALID_EMAIL'){
$ scope .user.error =Invalid Email;
}
else if(error.code =='EMAIL_TAKEN'){
$ scope.user.error =电子邮件已经在使用,if你认为这是一个错误联系管理员;
}
else {
$ scope.user.error =填写所有必填字段;
}
});
}
};
//从相机或照片库获取个人资料照片
$ scope.getPhoto = function(type){
$ ionicPlatform.ready(function(){
//图片质量/类型/大小/尺寸的选项
var options = {
quality:65,
destinationType:Camera.DestinationType.DATA_URL,
sourceType: Camera.PictureSourceType [type.toUpperCase()],
allowEdit:true,
encodingType:Camera.EncodingType.JPEG,
targetWidth:100,
targetHeight:100,
popoverOptions:CameraPopoverOptions,
saveToPhotoAlbum:false
};
//获取使用cordova-plugin-camera的图像函数
$ cordovaCamera.getPicture(options)
.then(function(photo){
$ scope.user.photo = photo;
},function(err){
console.log(err);
} );
});
};
}]);
以下是控制器正在使用的服务:
($'code>
.module('haulya.main')
.factory('User',function($ firebaseArray){
var ref =新的Firebase('https://haulya.firebaseio.com');
var users = $ firebaseArray(ref.child('profiles'));
var User = {
$ all:users,
create:function(user){
return users。$ add(user);
},
get:function(userId){
返回$ firebaseArray(ref.child('profiles')。child(userId));
},
delete:function(user){
return users。$ remove(user);
}
};
return User;
});
这也可以,但是我没有一个可靠的参考数组中的数据。对象ID只存储在控制器作用域。
我查看了其他帖子,但他们都使用旧版本的firebase和不赞成使用的方法。如果您要存储具有自然键的项目,最好将它们存储在该键下。对于用户来说,这将是 uid
。
因此,不要用 $ add ()
,用 child()。set()
。
create:function(user){
var userRef = users。$ ref()。child(user.uid);
userRef.set(user);
返回$ firebaseObject(userRef);
$ b $ p
$ b 你会注意到我正在使用非AngularFire方法 child()
和 set()
。 AngularFire是建立在Firebase常规JavaScript SDK之上的,所以它们很好地互操作。这样做的好处是,您可以使用Firebase JavaScript SDK的所有功能,并只使用AngularFire来实现它的最佳功能:将事物绑定到Angular的 $ scope
。
存储用户数据在Firebase的JavaScript指南中进行了解释。我们把它们保存在 uid
那里,而不是使用 push()
,这就是 $ add()
在幕后调用。
So I'm using Angularfire in an ionic app and trying to figure out how to make a user object that is associated with the auth data from an Auth $createUser call. My first try had the auth call and the user got authenticated, then a user object was made and pushed into a $firebaseArray
which works fine, but I don't know how to grab the current user after they are logged in to update, destory, or do anything with that users data. I have made it work with looping through the users array and matching the uid from the user array item and the auth.uid
item which was set to be the same in the user array object creation. This seems really ineffecient to loop over if there is a large user array and it needs to be done on multiple pages.
My current attempt is using a different method like so:
angular.module('haulya.main')
.controller('RegisterController', ['Auth', '$scope', 'User', '$ionicPlatform', '$cordovaCamera','CurrentUserService',
function(Auth, $scope, User, $ionicPlatform, $cordovaCamera, CurrentUserService) {
//scope variable for controller
$scope.user = {};
console.log(User);
$scope.createUser = function(isValid) {
var userModel;
$scope.submitted = true;
//messages for successful or failed user creation
$scope.user.message = null;
$scope.user.error = null;
//if form is filled out valid
if(isValid) {
//Create user with email and password firebase Auth method
Auth.$createUser({
email: $scope.user.email,
password: $scope.user.password
})
.then(function(userData) {
userModel = {
uid: userData.uid,
photo: $scope.user.photo || null,
firstName: $scope.user.firstName,
lastName: $scope.user.lastName,
email: $scope.user.email,
cell: $scope.user.cell,
dob: $scope.user.dob.toString(),
city: $scope.user.city,
state: $scope.user.state,
zip: $scope.user.zip
}
// add new user to profiles array
User.create(userModel).then(function(user) {
$scope.sharedUser = User.get(user.path.o[1]);
});
$scope.user.message = "User created for email: " + $scope.user.email;
})
.catch(function(error) {
//set error messages contextually
if(error.code == 'INVALID_EMAIL') {
$scope.user.error = "Invalid Email";
}
else if(error.code == 'EMAIL_TAKEN'){
$scope.user.error = "Email already in use, if you think this is an error contact an administrator";
}
else {
$scope.user.error = "Fill in all required fields";
}
});
}
};
//Get profile pic from camera, or photo library
$scope.getPhoto = function(type) {
$ionicPlatform.ready(function() {
//options for images quality/type/size/dimensions
var options = {
quality: 65,
destinationType: Camera.DestinationType.DATA_URL,
sourceType: Camera.PictureSourceType[type.toUpperCase()],
allowEdit: true,
encodingType: Camera.EncodingType.JPEG,
targetWidth: 100,
targetHeight: 100,
popoverOptions: CameraPopoverOptions,
saveToPhotoAlbum: false
};
//get image function using cordova-plugin-camera
$cordovaCamera.getPicture(options)
.then(function(photo) {
$scope.user.photo = photo;
}, function(err) {
console.log(err);
});
});
};
}]);
And here's the service the controller is using:
angular
.module('haulya.main')
.factory('User', function($firebaseArray) {
var ref = new Firebase('https://haulya.firebaseio.com');
var users = $firebaseArray(ref.child('profiles'));
var User = {
all: users,
create: function(user) {
return users.$add(user);
},
get: function(userId) {
return $firebaseArray(ref.child('profiles').child(userId));
},
delete: function(user) {
return users.$remove(user);
}
};
return User;
});
This also works, but again I don't have a solid reference to the currently logged in users object data from the array. The objects id is only stored on the controllers scope.
I looked through other posts, but they were all using older versions of firebase with deprecated methods.
解决方案 If you're storing items that have a "natural key", it is best to store them under that key. For users this would be the uid
.
So instead of storing them with $add()
, store them with child().set()
.
create: function(user) {
var userRef = users.$ref().child(user.uid);
userRef.set(user);
return $firebaseObject(userRef);
}
You'll note that I'm using non-AngularFire methods child()
and set()
. AngularFire is built on top of Firebase's regular JavaScript SDK, so they interoperate nicely. The advantage of this is that you can use all the power of the Firebase JavaScript SDK and only use AngularFire for what it's best at: binding things to Angular's $scope
.
Storing user data is explained in Firebase's guide for JavaScript. We store them under their uid
there too instead of using push()
, which is what $add()
calls behind the scenes.
这篇关于Firebase使用户对象来自验证数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!