I am using an initializer and deferReadiness() and advanceReadiness() to do the authentication. Works great.
I added a ready() function to the Application.
Next I want the application controller to do some stuff. Should I be calling a custom function in the application controller from the ready() function?
One suggestion, which I’ve been thinking of implementing in my own app, is to just let the Initializer inject the singleton object across the routes, controller, views. Then move you /api/current/me call into your application route’s model hook(s) and lazily set the properties on the user object.
This will allow you the ability to enter the loading substate and elegantly handle errors instead of being restricted to the “white screen of death” while your user current request resolves.
I’m not sure what you mean by authenticated, is sounds like you already have information to authenticate? Anyways, here is an example of an authenticated route that I extend from to require authentication by using the beforeModel callback:
var AuthenticatedRoute = Ember.Route.extend({
beforeModel: function(transition) {
var isAuthenticated = this.get('session.content.isAuthenticated');
if (!isAuthenticated) {
console.log("No user session, transitioning to login.");
var loginController = this.controllerFor('login');
loginController.set('previousTransition', transition);
this.transitionTo('login');
}
}
});
You might wonder what session is? Well, it is a object that I had ember inject into every route via dependency injection in an initializer. I also save the last transition attempted, so that I can retry it after the user authenticates since they are unauthenticated, so that they return to the route they were previously at.
Example on applying the previous transition after the user has authenticated:
previousTransition = this.get('previousTransition');
if (previousTransition) {
controller.set('previousTransition', null);
previousTransition.retry();
}