Last active
August 29, 2015 14:20
-
-
Save sunocean-sand/7833b6acd863cccbfc37 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import Firebase from 'firebase'; | |
var ref = new Firebase("https://nutella.firebaseio.com"); | |
export default Ember.Route.extend({ | |
actions: { | |
login: function() { | |
var controller = this; | |
controller.get("session").login(); | |
}, | |
loginFacebook: function() { | |
var controller = this; | |
controller.get("session").loginFacebook().then(function(user) { | |
}, function() { | |
}); | |
}, | |
loginTwitter: function() { | |
var controller = this; | |
controller.get("session").loginTwitter().then(function(user) { | |
}, function() { | |
}); | |
}, | |
logout: function() { | |
var controller = this; | |
controller.get('session').logout(); | |
}, | |
createUser: function() { | |
var controller = this; | |
controller.get('session').createUser().then(function(user) { | |
}, function() { | |
}); | |
}, | |
openModal: function(modal) { | |
this.render(modal, { | |
into: 'application', | |
outlet: 'modal' | |
}); | |
return Ember.run.schedule('afterRender', function() { | |
Ember.$('.modal').modal('show'); | |
}); | |
}, | |
closeModal: function() { | |
Ember.$('.modal').modal('hide'); | |
return this.disconnectOutlet({ | |
outlet: 'modal', | |
parentView: 'application' | |
}); | |
}, | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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://nutella.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); } | |
}); | |
}); | |
}, | |
createUser: function(email, password) { | |
var session = this; | |
return new Ember.RSVP.Promise(function(resolve, reject) { | |
session.get('ref').createUser(function(error, userData) { | |
if (userData) { | |
resolve(userData.uid); | |
//session.set("isNewUser", true); | |
alert("user created"); | |
} else { | |
reject(error); | |
alert("Error creating user"); | |
} | |
}); | |
}); | |
}, | |
logout: function() { | |
this.get("ref").unauth(); | |
}, | |
currentUser: Ember.computed('isAuthenticated', function() { | |
return this.get('ref').getAuth(); | |
}) | |
/* | |
currentUser: function() { | |
return this.get("ref").getAuth(); | |
}.property("isAuthenticated") | |
*/ | |
}); | |
export default { | |
name: "Session", | |
initialize: function (container, app) { | |
app.register("session:main", session); | |
app.inject("controller", "session", "session:main"); | |
app.inject("route", "session", "session:main"); | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment