Authentication like a Boss

  1. I want to authenticate every time the the application is loaded / reloaded, and re-route appropriately.

Where should the authentication occur? Is the place in the ready() function? or should I do a deferReadiness() after the App has been created?

I would really like to do it in the application controller, because I will be updating a model in the application route. Is that possible?

  1. How do I display the applications default Loading template while authenticating?

  2. And if authentication fails, should I not be showing the applications default Error template?


1 Like

Okay, so I am currently doing this:

  1. I am using an initializer and deferReadiness() and advanceReadiness() to do the authentication. Works great.

  2. I added a ready() function to the Application.

  3. 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.

1 Like

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);

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);
1 Like

thanks guys, I ended up implementing both of your ideas.

This can probably help you

1 Like

We’ve had good luck using this ember-auth framework