I am looking to satisfy 3 goals with my authentication in Ember.js using rails, development and cookie based session.
- Redirected to
#/sessions/new if they are not logged in. - Always show current user information in the application template.
- If the user is logged in and he goes directly to
#/some/route . The current user must be logged in at boot time.
I watched these videos with embercast: Client Side Authentication Part 1 and Client Side Authentication Part 2 . They are a bit outdated, but useful.
But still there can be no complete solution. Does anyone have full Rails 4, Devise, Emberjs 1.0.0?
The biggest problem is the strategy of loading the current user to load the page and setting the current user when submitting the form.
Now this is my strategy:
App.User = Em.Object.extend(); App.User.reopenClass({ current: function() { return Ember.$.getJSON("/users/current").then(function(data) { return data }) } }); App.ApplicationRoute = Ember.Route.extend({ model: function() { return App.User.current(); } }); App.SessionsNewController = Ember.ObjectController.extend({ actions: { save: function(data) { var self = this, data = this.getProperties('email', 'password'); $.post("/sessions", { session: data }).always(function(response, status, data) { if (status == "success") { self.transitionToRoute('index'); } else { self.set('errorMessage', data); } }) }, } });
source share