recommended way to access ref to create user?

173 views
Skip to first unread message

Matt

unread,
May 1, 2016, 1:05:14 PM5/1/16
to Firebase + EmberJS
Might seem like a silly question, but what is the best way to access the native methods in firebase?

For example, the code example on Firebase shows: 



var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");

  • ref.createUser({
  •  email: "not_a_real_user@new-domain-test-123.com",
  •  password: "correcthorsebatterystaple"
  • }, function(error, userData) {
  •  if (error) {
  •    switch (error.code) {
  •      case "EMAIL_TAKEN":
  •        console.log("The new user account cannot be created because the email is already in use.");
  •        break;
  •      case "INVALID_EMAIL":
  •        console.log("The specified email is not a valid email.");
  •        break;
  •      default:
  •        console.log("Error creating user:", error);
  •    }
  •  } else {
  •    console.log("Successfully created user account with uid:", userData.uid);
  •  }
  • });




How does this translate to EmberFire?  Meaning, if I want to create a new user, I know I would simply do this:




      var newUser = this.store.createRecord('user', {
        user
: this.get('user'),
        password
: this.get('password'),
        timestamp
: new Date().getTime()
     
});
      newUser
.save();




How would I get this to interact with Firebase's native createUser method?





Tim Stirrat

unread,
May 4, 2016, 12:43:35 AM5/4/16
to Matt, Firebase + EmberJS
Hi Matt,

If you want to interact with the base Firebase reference you can inject the 'firebase' service like this:


export default Ember.Controller.extend({
  firebase: Ember.inject.service(),

  actions: {
    
    createUser() {
      const ref = this.get('firebase');
      ref.createUser({
        email: this.get('email'),
        password: this.get('password')
      }, (err, authData) => {
        if (err) {
          // do something about this error
        } else {
          // create a db record for your user
        }
      });
    }

  }  
});

Note that this will create an email/password user for Firebase Auth, but will not create the user object in your database. For that, you would additionally use this.store.createRecord.

Hope that helps!

Tim

--
You received this message because you are subscribed to the Google Groups "Firebase + EmberJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-embe...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Regards,

Tim Stirrat
Reply all
Reply to author
Forward
0 new messages