Skip to content

Instantly share code, notes, and snippets.

@mbbertino
Last active August 29, 2015 14:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbbertino/5ac801c22fa9f3748a49 to your computer and use it in GitHub Desktop.
Save mbbertino/5ac801c22fa9f3748a49 to your computer and use it in GitHub Desktop.
Ember CLI, EmberFire, Firebase Simple Login Email/Password Protected Routes
// 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
}
});
@jfrux
Copy link

jfrux commented Mar 20, 2015

Anybody have any examples with the new Firebase version that includes Login within it? I'm looking to find examples without the legacy SimpleLogin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment