Last active
August 29, 2015 14:05
-
-
Save mbbertino/5ac801c22fa9f3748a49 to your computer and use it in GitHub Desktop.
Ember CLI, EmberFire, Firebase Simple Login Email/Password Protected Routes
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
// Router | |
import Ember from 'ember'; | |
var Router = Ember.Router.extend({ | |
location: ClientENV.locationType | |
}); | |
Router.map(function() { | |
this.resource('login'); | |
this.resource('secretRouteToBeProtected'); | |
... | |
}); | |
export default Router; | |
// Auth initializers/auth.js | |
import Ember from 'ember'; | |
var auth = Ember.Object.extend({ | |
authed: false, | |
currentUser: null, | |
init: function() { | |
this.authClient = new window.FirebaseSimpleLogin(new window.Firebase("https://<Your firebase link>.firebaseio.com"), function(error, user) { | |
if (error) { | |
alert('Authentication failed: ' + error); | |
} else if (user) { | |
this.set('authed', true); | |
this.set('currentUser', user) | |
} else { | |
this.set('authed', false); | |
} | |
}.bind(this)); | |
}, | |
login: function(email, password) { | |
this.authClient.login('password', { | |
email: email, | |
password: password | |
// rememberMe: remember | |
}); | |
}, | |
logout: function() { | |
this.authClient.logout(); | |
}, | |
createUser: function(email, password){ | |
this.authClient.createUser(email, password, function(error, user) { | |
if (error === null) | |
{console.log("User created successfully:", user);} | |
else | |
{console.log("Error creating user:", error);} | |
}); | |
} | |
}); | |
export default { | |
name: 'Auth', | |
initialize: function( container, app ) { | |
app.register('auth:main', auth, {singleton: true}); | |
app.inject('controller', 'auth', 'auth:main'); | |
app.inject('route', 'auth', 'auth:main'); | |
} | |
}; | |
// login route | |
import Ember from "ember"; | |
export default Ember.Route.extend({ | |
init: function () { | |
var auth = this.get('auth') | |
console.log(auth) // this returns an object where authed is true and currentUser is an object | |
console.log(auth.authed) // this returns false | |
console.log(auth.currentUser) // this returns null | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Anybody have any examples with the new Firebase version that includes Login within it? I'm looking to find examples without the legacy SimpleLogin