Last active
August 29, 2015 14:06
-
-
Save gpbmike/e5135324a716667e7587 to your computer and use it in GitHub Desktop.
Webhook Authentication Snippets
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
App.ApplicationController = Ember.Controller.extend({ | |
userStatusChanged: function () { | |
var previousTransition = this.get('session.transition'); | |
// if you were trying to get somewhere, try again | |
if (previousTransition) { | |
Ember.Logger.log('Retrying route `%@`.'.fmt(previousTransition.targetName)); | |
if (previousTransition.targetName === this.get('currentPath')) { | |
this.send('refreshRoute'); | |
} else { | |
previousTransition.retry(); | |
} | |
} | |
else if (this.get('currentPath') === 'login') { | |
this.transitionToRoute('index'); | |
} | |
else { | |
this.send('refreshRoute'); | |
} | |
}.observes('session.user') | |
}); |
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
Ember.Route.reopen({ | |
beforeModel: function (transition) { | |
// These routes you do not need to be logged in to access. | |
var openRoutes = ['login']; | |
// Not logged in and attempting to access protected route, redirect to login. | |
if (openRoutes.indexOf(transition.targetName) === -1 && Ember.isEmpty(this.get('session.user'))) { | |
Ember.Logger.warn('Attempting to access protected route when not logged in. Aborting.'); | |
// Save the transition to try again status changes. | |
this.set('session.transition', transition); | |
// Redirect to login. | |
this.transitionTo('login'); | |
} | |
} | |
}); |
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
Ember.Application.initializer({ | |
name: 'login', | |
initialize: function (container, application) { | |
application.deferReadiness(); | |
var session = Ember.Object.create(); | |
var auth = new FirebaseSimpleLogin(new Firebase('https://webhook-examples.firebaseio.com/login/'), function (error, user) { | |
if (error) { | |
Ember.Logger.error(error); | |
session.set('error', error); | |
} else if (user) { | |
Ember.Logger.log('Logged in as %@'.fmt(user.uid)); | |
session.set('user', user); | |
} else { | |
Ember.Logger.log('Not logged in.'); | |
session.set('user', null); | |
} | |
Ember.run(application, application.advanceReadiness); | |
}); | |
// use to access Simple Login methods ie: route.get(`session.auth`).logout() | |
session.set('auth', auth); | |
application.register('login:session:current', session, { instantiate: false, singleton: true }); | |
// Add `session` object to route to check user | |
application.inject('route', 'session', 'login:session:current'); | |
// Add `session` object to controller to visualize in templates | |
application.inject('controller', 'session', 'login:session:current'); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment