Created
October 27, 2014 17:42
-
-
Save jacobthemyth/bea5c7303d019fed7d02 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
(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); | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hey, this is great. Do you have a newer version of this? Just asking before I try it out.