Created
May 20, 2014 17:53
-
-
Save zero-is-one/3019a2e591917c3e7f3e to your computer and use it in GitHub Desktop.
Emberfire Session Object
This file contains 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
//help | |
// http://stackoverflow.com/questions/18209862/how-and-when-to-use-ember-application-register-and-inject-methods | |
// http://webcloud.github.io/blog/2014/04/07/emberjs-authentication-the-right-way-javascript-version/ | |
// https://github.com/andrewreedy/ember-session | |
App.Session = Ember.Object.extend({ | |
firebaseSimpleLogin: null, | |
user: null, | |
init:function(){ | |
var _this = this; | |
this.set('store', this.container.lookup('store:main')); | |
this.auth() | |
}, | |
auth: function(){ | |
var _this = this; | |
return new Ember.RSVP.Promise(function(resolve, reject) { | |
if (_this.get('authObj')) return resolve(_this.get('authObj')) | |
var auth = new FirebaseSimpleLogin(App.Firebase, function(error, user) { | |
if (error) { | |
return reject(error) | |
}else{ | |
_this.set('authObj', auth) | |
_this.load(user) | |
return resolve(_this.get('authObj')) | |
} | |
}); | |
}); | |
}, | |
load: function(user){ | |
var _this = this; | |
if (user) { | |
_this.set('firebaseSimpleLogin', user) | |
_this.store.find('user', user.id).then(function(){ | |
_this.set('user', _this.get('store').getById('user', user.id) ); | |
}) | |
} | |
}, | |
loginEmail: function(credentials){ | |
var _this = this; | |
return new Ember.RSVP.Promise(function(resolve, reject) { | |
_this.auth().then(function(auth){ | |
auth.login('password', credentials) | |
.then(function(user){ | |
return resolve(_this.load(user)); | |
}) | |
.catch(function(error){ | |
return reject(error); | |
}) | |
}); | |
}); | |
}, | |
signUpEmail: function(credentials){ | |
var _this = this; | |
return new Ember.RSVP.Promise(function(resolve, reject) { | |
if (credentials.password != credentials.confirm){ | |
return reject("Passwords do not match."); | |
} | |
_this.auth().then(function(auth){ | |
auth.createUser(credentials.email, credentials.password, function(error, user) { | |
if (!error) { | |
Ember.run(function() { | |
_this.set('user', _this.get('store').createRecord('user',{ | |
uid: user.uid, | |
id: user.id, | |
email: user.email, | |
nicename: "Anon" | |
})) | |
_this.get('user').save().then(function(){ | |
_this.loginEmail(credentials); | |
}); | |
resolve(true); | |
}); | |
}else{ | |
Ember.run(function() { | |
console.log(error) | |
reject(error.message); | |
}); | |
} | |
}); | |
}); | |
}); | |
}, | |
logout:function(){ | |
this.set('firebaseSimpleLogin',null); | |
this.set('user',null); | |
this.auth().then(function(auth){ | |
auth.logout(); | |
}) | |
}, | |
test:function(){ | |
this.get('user').get("courses").pushObject( | |
this.get('store').createRecord('course', { | |
title: 'Rails is Omakase' | |
}) | |
); | |
console.log(this.get('user').save()) | |
}, | |
isAuthenticated: function() { | |
return !Ember.isEmpty(this.get('firebaseSimpleLogin')); | |
}.property('firebaseSimpleLogin'), | |
isAuthenticatedDirtyCheck: function() { | |
return !(localStorage.getItem("firebaseSession") === null); | |
}.property('firebaseSimpleLogin') | |
}); | |
Ember.Application.initializer({ | |
name: 'session', | |
initialize: function (container, application) { | |
// Register the session | |
App.register('session:current', App.Session, { singleton: true }); | |
// Inject the session into all controllers, views, routes, models, and components | |
Ember.A(['model', 'controller', 'view', 'route', 'component']).forEach(function(component) { | |
App.inject(component, 'session', 'session:current'); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Did this end up working?