Authentication with extra userdata

24 views
Skip to first unread message

Tamas Amon

unread,
Apr 7, 2016, 1:55:48 PM4/7/16
to Firebase + EmberJS
Hello!

I made a facebook authentication on my Ember app, but I like to save some datas about the logged in user. I made a model:

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  uid:  DS.attr('string'),
  type: DS.attr('string'),
  other: DS.attr(),
  role: DS.attr('string')
});

The uid is what I get back when the user is azthenticated.

// The data is what I get back from facebook  
signIn(data) {
    let user;
      user = {
        id:     data.uid,
        name:   data.currentUser.displayName,
        userid: data.uid,
        type:   data.provider,
        other:  data,
        role:   'user'
    };
this.store.createRecord('user', user).save()
}


The problem is now every time if the user logged in the app makes a new user. How can I prevent it?
Or if there is a good tutorial about user registration somewhere it will be very nioce

--
Tamas Amon

Tim Stirrat

unread,
Apr 7, 2016, 5:09:57 PM4/7/16
to Tamas Amon, Firebase + EmberJS
Hey Tamas, 

Sounds like you need a "find or create" pattern. First, search for the user, and if it is not found, create a new one:

return store.find('user', data.uid)
  .then(function(u) {
    console.log('found user with uid', data.uid);
    return u;
  })
  .catch(function () {
    console.log('creating user for uid', data.uid);
    const user = {
      id:     data.uid,
      name:   data.currentUser.displayName,
      type:   data.provider,
      other:  data,
      role:   'user'
    };
    return store.createRecord('user', user);
  })
  .then(function (u) {
   // u = found or created user
   console.log('found or created', u);
   return u;
  });

This github issue details a workaround that you might need, depending on which version of ember-data you're using.

Hope that gets you on a good path,

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