Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(function(){
'use strict';
window.ENV = window.ENV || {};
window.ENV['simple-auth-firebase'] = {
firebase: new Firebase("https://blistering-torch-3318.firebaseio.com/")
};
var defaults = {
firebase: null,
resourceName: 'users'
};
var Configuration = {
firebase: null,
resourceName: defaults.resourceName,
load: SimpleAuth.Utils.loadConfig(defaults)
};
var FirebasePasswordAuthenticator = SimpleAuth.Authenticators.Base.extend({
init: function(){
this.firebase = Configuration.firebase;
this.resourceName = Configuration.resourceName;
if (!this.firebase || typeof this.firebase !== 'object') {
throw new Error('Please set the `firebase` property on the simple-auth-firebase config.');
}
// If provided Firebase reference was a query (eg: limits), make it a ref.
this._ref = this.firebase.ref();
this._super();
},
restore: function(data) {
var self = this;
return new Ember.RSVP.Promise(function(resolve, reject){
self._ref.authWithCustomToken(data.token, function(error, authData){
if(error) {
Ember.run(function(){
reject(error);
});
} else {
Ember.run(function(){
resolve(authData);
});
}
}, function(error){
console.error(error);
});
});
},
authenticate: function(credentials){
var self = this;
return new Ember.RSVP.Promise(function(resolve, reject){
self._ref.authWithPassword(credentials, function(error, authData){
if(error) {
Ember.run(function(){
reject(error);
});
} else {
Ember.run(function(){
resolve(authData);
});
}
});
});
}
});
Ember.FirebaseUserController = Ember.Controller.create({
actions: {
createUser: function(credentials){
var data = this.getProperties('email', 'password');
var ref = Configuration.firebase.ref();
var self = this;
ref.createUser(data, function(error){
self.set('createUserError', error);
if( ! error ) {
self.get('session').authenticate('authenticator:firebase-password', data).then(function(){
var authData = self.get('session.content');
ref.child('users').child(authData.uid).set(authData);
});
}
});
}
}
});
Ember.Application.initializer({
name: 'firebase-authenticator',
before: 'simple-auth',
initialize: function(container, application) {
var config = SimpleAuth.Utils.getGlobalConfig('simple-auth-firebase');
Configuration.load(container, config);
container.register('authenticator:firebase-password', FirebasePasswordAuthenticator);
}
});
})();
@oskarrough

This comment has been minimized.

Copy link

@oskarrough oskarrough commented Mar 12, 2015

hey, this is great. Do you have a newer version of this? Just asking before I try it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.