Storing the correct uid for firebase social authentication


#1

I am trying to store the uid in session data into a model in Ember js, using Firebase.

Somehow, what I’m doing to take session data and store into model in application.js seems to be taking out the “provider” part of the uid. Weirdly I have 2 user data models, “123425” is storing user’s name from twitter and “twitter:1234132” is not storing any user’s information.

application.js

import Ember from 'ember'; export default Ember.Route.extend({

model: function() {
    var user = this.get('session.uid');
    if (user) {
        return this.store.find('user', user);
    } else  {
        return null;
    } },

actions: {

    loginFacebook: function() {
        var controller = this;
            controller.get("session").loginFacebook().then(function(user) {
                console.log(user);
            });
        this.send('dismiss');
    },

initializer

import Ember from 'ember';
import Firebase from 'firebase';


function parseAuthData(authData) {
    var parsedData = {};
    switch(authData.provider) {

        case 'facebook':
            parsedData.provider = authData.provider;
            parsedData.id = authData.facebook.id;
            parsedData.displayName = authData.facebook.displayName;
            parsedData.gender = authData.facebook.cachedUserProfile.gender;
            parsedData.language = authData.facebook.cachedUserProfile.locale;
            parsedData.imageThumbUrl = authData.facebook.cachedUserProfile.picture.data.url;
            parsedData.website = authData.facebook.cachedUserProfile.link;
            return parsedData;

        case 'twitter':
            parsedData.provider = authData.provider;
            parsedData.id = authData.twitter.id;
            parsedData.username = authData.twitter.username;
            parsedData.displayName = authData.twitter.displayName;
            parsedData.description = authData.twitter.cachedUserProfile.description;
            parsedData.location = authData.twitter.cachedUserProfile.location;
            parsedData.language = authData.twitter.cachedUserProfile.lang;
            parsedData.imageThumbUrl = authData.twitter.cachedUserProfile.profile_image_url_https || authData.twitter.cachedUserProfile.profile_image_url;
            parsedData.website = authData.twitter.cachedUserProfile.url;
            return parsedData;
    }
}


var session = Ember.Object.extend({
    ref : new Firebase("https://id.firebaseio.com"),

    addFirebaseCallback: function() {
        var session = this;
        var ref = this.get('ref');

        ref.onAuth(function(authData) {
            if (authData) {
                var user = parseAuthData(authData);
                session.set("isAuthenticated", true);
                session.set('uid', authData.uid);
                session.set('user', user);
                ref.child('users').child(authData.uid).set(user);

            } else {
                session.set("isAuthenticated", false);
            }
        });
    }.on("init"),


    loginFacebook: function() {
        var session = this;
        return new Ember.RSVP.Promise(function (resolve, reject) {
            session.get("ref").authWithOAuthPopup("facebook", function(error, user) {
                if (user) {
                    resolve(user);
                } else {
                    reject(error);
                }
            },

            {
                remember: "sessionOnly",
                scope: "email"
            });
        });
    },

    loginTwitter: function() {
            var session = this;
            return new Ember.RSVP.Promise(function(resolve, reject) {
                session.get('ref').authWithOAuthPopup('twitter', function(error, user) {
                    if (user) { resolve(user); }
                    else { reject(error); }
                });
            });
    },