AngularFire: Getting the ID of Users Data from email in SimpleLogin - angularjs

I'm trying to implement some simple using registration using Firebase through AngularFire and Angular.js. I'm using the SimpleLogin tool to manage the users. I can create users just fine.
var firebaseRef = new Firebase(FIREBASE_URL);
var simpleLogin = $firebaseSimpleLogin(firebaseRef);
var firebaseUsersRef = new Firebase(FIREBASE_URL + 'users');
var firebaseUsers = $firebase(firebaseUsersRef);
var myObject = {
register: function(user) {
var myDate = new Date().getTime();
return simpleLogin.$createUser(
user.email, user.password)
.then(function(regUser) {
var userInfo = {
date: myDate,
md5: regUser.md5_hash,
firstname: user.firstname,
lastname: user.lastname,
email: user.email
}
firebaseUsers.$push(userInfo).then(function(ref) {
userInfo.uid = ref.name();
$rootScope.currentUser = userInfo;
});
}); //push user
}, //register
Works like a charm. In order to get at this information when the user logs in, I've tried implementing an event handler on the $rootscope. I would like it to search through the uid that I stored and then get me record with the right user information.
$rootScope.$on('$firebaseSimpleLogin:login', function (e, authUser) {
var query = $firebase(firebaseRef.startAt(authUser.uid).endAt(authUser.uid));
console.log(query);
$location.path('/meetings');
});
In order to use startAt and endAt, do I have to establish $priority. When I try, I get an error stating that I can't have any special characters. So that never works. I don't really care about how this data stored, I just want to get the index of the data so that I can retrieve the right user.

By using $push you tell Firebase to generate a key for you.
This is great for collections where you normally access all children at the same time. But that is not the case for your user info: you want to access the info for the current user.
So instead of using $push to add your user's info, I would use the uid of the user.
In the regular Firebase JavaScript API this can be accomplish with:
firebaseUsersRef.child(reguser.uid).set(userInfo);
The equivalent in AngularFire probably uses $set, but I don't think you have any need for that in your $createUser callback.
Update
It looks like you're trying to add your info to the existing user node that Firebase creates for you. This is the example from that from the Firebase documentation on storing user data:
myRef.child('users').child(user.uid).set({
displayName: user.displayName,
provider: user.provider,
provider_id: user.id
});
You can see that they do access the user's node using child(user.uid) similar to what I proposed.
Lessons
Two relatively small mistakes here as far as I can see:
when you use push/$push, you let Firebase generate the node name for you. In cases where there already is a globally unique ID (such as the uid of a user), you're often better off using that as the node name.
If you know the name of the node you want to retrieve, you don't need a query. You can simply access the node as ref.child(user.uid).

Thanks to Frank, I was able to figure out the right way to do this. In order to make my own users object searchable, I can use the uid from the simpleLogin object. So my register function works like this:
var firebaseRef = new Firebase(FIREBASE_URL);
var simpleLogin = $firebaseSimpleLogin(firebaseRef);
var myObject = {
register: function(user) {
var myDate = new Date().getTime();
return simpleLogin.$createUser(user.email, user.password)
.then(function(regUser) {
var userInfo = {
date: myDate,
md5: regUser.md5_hash,
firstname: user.firstname,
lastname: user.lastname,
email: user.email
}
firebaseUsers.$set(regUser.uid, userInfo);
}); //add user
}, //register
} //my Object
Using set instead of push, I can store the uid from the registered user into the object and then pass along what I want to add as the second parameter. My database will now have the users organized by uid, which can be accessed via a url.
Then, when users log in, Firebase will throw up a login event along with the authenticated user, which I can catch and use to add the current user to the $rootScope that's accessible throughout my application.
$rootScope.$on('$firebaseSimpleLogin:login', function (e, authUser) {
var ref = new Firebase(FIREBASE_URL + '/users/' + authUser.uid);
var user = $firebase(ref).$asObject();
user.$loaded().then(function() {
$rootScope.currentUser = user;
});
$location.path('/meetings');
});
Thanks for pointing me in the right direction Frank.

Related

Getting Username from Firebase after email/password login [React Native]

I have created a simple login app using react native that let's users signup, login, and logout. my signup function takes a username that is then used in the createUser callback to generate a db entry with the uid as the key, and the username entered as a value. The answer in this post is the structure I followed - How do you include a username when storing email and password using Firebase (BaaS) in an Android app?
After the user is logged in, I'd like to get the username and display it but I'm having trouble figuring this out.
This is the code I currently have to attempt and do it:
var ref = new Firebase("https://myreactapp.firebaseio.com");
module.exports = React.createClass({
getInitialState: function() {
var authData = ref.getAuth();
var user = ref.child("users/" + authData.uid + "/username");
return {
username: user
};
},
This is how the code looks when I signup and the structure of my db.
var self = this;
let ref = new Firebase("https://myreactapp.firebaseio.com");
ref.createUser({
email : this.state.email,
password : this.state.password
}, function(error, authData) {
if (error) {
return this.setState({errorMessage: 'Error creating user'});
} else {
ref.child("users").child(authData.uid).set({
username: self.state.username
});
console.log("Successfully created user account with uid:", authData.uid);
ToastAndroid.show('Account Created', ToastAndroid.SHORT)
return self.props.navigator.pop();
}
});
----------------Not actual code-------------------------------------------------
DB
+users
--<uid>
-username -> value
--<uid>
-username -> value
I try to login and get an error of maximum call stack exceeded, but I have a feeling I'm going at this the wrong way. I've looked online, but everything I found was for retrieving data that is either being added, changed or deleted. All I want is to get this username once.

Saving user information to Firebase

I am new to Firebase and am currently setting up user and user registration. I have got that working fine but I want to save user information to my firebase database. This is my register function
register: function(user){
return simpleLogin.$createUser(user.email, user.password)
.then(function(regUser){
var ref = new Firebase(FIREBASE_URL + 'users');
var firebaseUsers = $firebase(ref);
var userInfo = {
date: Firebase.ServerValue.TIMESTAMP,
regUser: regUser.uid,
firstname: user.firstname,
lastname: user.lastname,
email: user.email
}
firebaseUsers.$set(regUser.uid, userInfo);
});//add user
},//register
As you can see from the above, I have created a userInfo object that I want to add to my database. What I want to happen is that when you go into my database I have a tree structure of
user - uid - userinfo
Instead of the above I have users - and all you can see is the uid:
I have debugged the code and all the info is being properly saved to my userInfo object - its just doesn't seem to save to firebase properly

Firebase AngularFire using $priority and the $save overwrites the object

Based on the AngularFire documentation I'm trying to set $priority on an object, then use $save() to update the data on Firebase.
The result I get instead is the object getting overwritten as empty (it's entry in Firebase).
Here is my code
var ref = new Firebase(firebaseListPath);
var myList = $firebase(ref).$asArray();
myList.$add({
created: Firebase.ServerValue.TIMESTAMP,
user: userId
}).then(function(newItemRef){
var newItemObj = $firebase(newItemRef).$asObject();
newItemObj.$priority = userId;
newItemObj .$save();
});
Try passing the priority directly into $add:
myList.$add({
created: Firebase.ServerValue.TIMESTAMP,
user: userId,
$priority: userId
});
There is also no reason to call $asArray and $asObject on the same synchronized data (it's already synchronized). This would work in your current model, but be unnecessary:
myList.$add({
created: Firebase.ServerValue.TIMESTAMP,
user: userId
}).then(function(newItemRef){
var item = myList.$getRecord(newItemRef.name());
item.$priority = userId;
myList.$save(item);
});
However, there is really no reason to be using AngularFire here. If you aren't binding anything to the view, then you can simply make the call directly to the ref:
var ref = new Firebase(firebaseListPath);
ref.push().setWithPriority({ ... }, userId);

How to do a query using dot( . ) through Mongoose in Node.js and How to add an empty array

I have the following schema:
var userSchema = new Schema({
userID: Number,
userName: String,
userEmail: String,
teams:Array,
socialMedias:
{
fbUID: String,
googleUID: String,
twitter: String }
});
First, How can I add an empty array? Is it right the way I am doing in the following?
teams:{},
Second, I am trying to do a query using Mongoose in my Node.js but I am getting an error in the dot ('.'):
This is my document I am saving:
var user = new users({
userID: id, //give the id of the next user in Dbase
userName: userName,
userEmail: 'userEmail',
teams:{},
socialMedias: [{socialMediaType: socialMediaID}]
});
where userName, socialMediaType and socialMediaID are parameters of a function.
So, after I add this doc, I am trying to do the following query:
function searchUser(socialMediaID, socialMediaType){
var user
users.findOne({socialMedias.socialMediaType: socialMediaID}, function(err, userFound){
if(err) return handleError(err);
user = userFound;
});
//what does MongoDb return if it does not find the document?
return user;
}
but I am getting an error in this :
socialMedias.socialMediaType
So, how can I do this query?
I tried to find in Mongoose Documentation but I did not find.
Thank you for your understanding.
There's a number of issues here that you are likely running into.
First, teams is an array property, but you're assigning an object to it. You need to do something like this:
var user = new users({
userID: id, //give the id of the next user in Dbase
userName: userName,
userEmail: 'userEmail',
teams:[],
socialMedias: [{socialMediaType: socialMediaID}]
});
Second, if socialMediaType is passed in as a function param, you can't use it like you're doing. You need to do something like this:
var socialMedias = {};
socialMedias[socialMediaType] = socialMediaID;
var user = new users({
userID: id, //give the id of the next user in Dbase
userName: userName,
userEmail: 'userEmail',
teams:[],
socialMedias: [socialMedias]
});
Third your findOne is not going to work as is. From what I can gather of your intention here, you need something like this:
function searchUser(socialMediaID, socialMediaType){
var user
var query = {};
query["socialMedias."+socialMediaType] = socialMediaID;
users.findOne(query, function(err, userFound){
if(err) return handleError(err);
user = userFound;
});
//what does MongoDb return if it does not find the document?
return user;
}
But fourth, even that won't work because you are synchronously returning user from a method that performs and asynchronous operation. There are various ways to solve that, but you can start by reading up about promises, or passing a callback function into searchUser.

firebase/angularFire querying with startAt/endAt doesn't work

I have an app where I use FirebaseSimpleLogin, but since I have additional user data, I store it under [my-firebase].firebaseio.com/users/[username]. The snippet below shows how it's done
var User = {
create: function (authUser, username) {
users[username] = {
md5_hash: authUser.md5_hash,
username: username,
email: authUser.email,
$priority: authUser.uid
};
users.$save(username).then(function () {
setCurrentUser(username);
});
},
...
Since data for each individual user, are keyed based on username I prioritize by uid, so I can later fetch additional user data by uid.
When the firebase login event fires, I have the following handler, that is responsible for querying firebase to get the additionala user data, and store it on $rootScope.currentUser via a method setCurrentUser(username)
Here is my login event handler:
var ref = new Firebase(FIREBASE_URL + '/users');
var users = $firebase(ref);
$rootScope.$on('$firebaseSimpleLogin:login', function (e, authUser) {
var query = $firebase(ref.startAt(authUser.uid).endAt(authUser.uid));
query.$on('loaded', function () {
//PROBLEM: console.log(query.$getIndex()) logs an empty array
setCurrentUser(query.$getIndex()[0]);
});
});
As soon as the login event fires, I get access to the authUser, which contains the uid of the logged in user. Then I query firebase /users by uid, using startAt/endAt to limit my results so that I'm left only with the currently logged in user.
Then, when the query data is loaded from firebase, I invoke the setCurrentUser method which stores the username of the currentUser on $rootScope.
The Problem
The query filtering using startAt/endAt does not work, I'm getting back an empty array when I console.log query.$getIndex() when I should be getting an array with the username of the currently logged-in user.
I'm using firebase 1.0.15, angularFire 0.7.1 and firebase-simple-login 1.4.1 and following this tutorial from Thinkster.io
In the end somewhere in my code I had a $save() on my user binding, to save the id of the last project they've been working on, and this was causing the $priority issue. I used $update instead, and now everything works like a charm!
Still I don't know if this is intended behavior of $save()
To clarify and provide more context, I wasn't passing any keys to the $save() method, I simply added a property on my local user reference, and saved it
$rootScope.currentUser.lastWorkingProject = projectId;
$rootScope.currentUser.$save();

Resources