Skip to content

Instantly share code, notes, and snippets.

@guyjacks
Created February 12, 2015 19:43
Show Gist options
  • Save guyjacks/c279518d27abc7c20500 to your computer and use it in GitHub Desktop.
Save guyjacks/c279518d27abc7c20500 to your computer and use it in GitHub Desktop.
// #### I'm using this code to create a user
// #### User's have a User record and a profile record
// My factories for interacting w/ firebase are at the bottom
$scope.createUser = createUser = function() {
// Log the new user in and create a profile
var pass = function(newUser){
// create a profile for the new user
var pass = function (authData) {
var uid = authData.uid;
var provider = authData.provider;
var profile = Profiles.add({first:"", last:""});
profile.then(function(ref) {
User.set(uid, provider, ref.key());
}, function(err) {
console.log('profile err', err);
});
$window.location.href = '/dash';
}
// handle errors for authenitcating the new user
var fail = function (error) {
console.error('Authentication failed', error);
}
// Log the new user in
var promise = Auth.$authWithPassword({
email: $scope.email,
password: $scope.password
});
promise.then(pass, fail);
};
// handle errors when creating the new user fails
var fail = function(error){
console.error('Create new user failed', error);
};
// create the new user
var promise = Auth.$createUser({
email: $scope.email,
password: $scope.password
});
promise.then(pass, fail);
};
// #### This is the code where I'm trying to read the data
$scope.authData = authData = Auth.$getAuth();
var user = User.getProfile(authData.uid);
var profileId = user.profile;
Profile.get(profileId).$bindTo($scope, "profile");
console.log($scope.profile.first, $scope.profile.last);
// #### My Factories
app.factory("User", ["$firebase", "fbUrl", function($firebase, fbUrl) {
var ref = new Firebase(fbUrl + 'users');
var get = function(uid) {
var userRef = ref.child(uid);
//return $firebase(userRef).$asObject()
return $firebase(userRef).$asObject();
}
var getProfile = function(uid) {
var userRef = ref.child(uid);
//return userRef;
var userRef = new Firebase("radiant-fire-577.firebaseio.com/users/simplelogin%3A40/profile");
return $firebase(userRef).$asObject();
};
var set = function(uid, provider, profile) {
var userRef = ref.child(uid);
var user = $firebase(userRef);
return user.$set({
provider: provider,
profile: profile
});
};
return {
get: get,
getProfile: getProfile,
set: set
};
}]);
app.factory("Users", ["$firebase", "fbUrl", function($firebase, fbUrl) {
var ref = new Firebase(fbUrl + 'users');
var users = $firebase(ref).$asArray();
var get = function() {
return users;
};
var getRecord = function(id) {
return users.$getRecord(id);
}
var update = function(id) {
users.$save(id);
};
var remove = function(id) {
users.$remove(id);
};
return {
get: get,
getRecord: getRecord,
update: update,
remove: remove
};
}]);
app.factory("Profile", ["$firebase", "fbUrl", function($firebase, fbUrl) {
var ref = new Firebase(fbUrl + 'profiles');
var get = function(id) {
var profileRef = ref.child(id);
return $firebase(ref).$asObject()
};
return {
get: get
}
}]);
app.factory("Profiles", ["$firebase", "fbUrl", function($firebase, fbUrl) {
var ref = new Firebase(fbUrl + 'profiles');
var profiles = $firebase(ref).$asArray();
var get = function() {
return profiles;
};
var add = function(profile) {
return profiles.$add(profile);
};
return {
get: get,
add: add
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment