UPDATE: As @DustMason says in his answer, check out the amazing embercasts for best authentication methods.
To completely separate the view (Ember application) from the server (Rails application), I want to use token authentication. I will most likely be using Devise on a Rails server.
Has the meaning.
I need something like the before_filter equivalent in an Ember application where I can check if the current user is there and if that user has an authentication token set.
You can add the enter hook to the routes, this is roughly equivalent to the value of before_filter. But not sure what the best place to check for a token-original.
The Rails server returns the current authentication token on every call.
Has the meaning. We use cookie-auth and select the current user profile by calling /api/me , but both should work.
If it returns a zero authentication token, the Ember application should detect this and go into an unauthorized access state, redirecting it to the login window.
The fact is that (unlike rails) it is not easy to "protect" access to certain routes of embers. And regardless of the fact that the user can always open the open JS console and enter any state they want. Therefore, instead of thinking: "the user can enter this state only if he has passed authentication", think "what if an unauthorized user somehow goes to this route"
I suspect that for this I should use the Ember state machine, but I'm not sure how to proceed. Has anyone else solved this problem?
Our authorization needs are quite simple, so we did not find the right destination machine. Instead, we have the isAuthenticated property on the ApplicationController. We use this property in application.hbs to replace the main view with a login form when the user is not authenticated.
{{if isAuthenticated}} {{render "topnav"}} {{outlet}} {{else}} {{render "login"}} {{/if}}
From ApplicationRoute we get the user profile:
App.ApplicationRoute = Ember.Route.extend({ model: function() { var profiles; profiles = App.Profile.find({ alias: 'me' }); profiles.on("didLoad", function() { return profiles.resolve(profiles.get("firstObject")); }); return profiles; } });
Our ApplicationController then calculates the isAuthenticated property based on the returned profile.