33

I was looking up documentation and posts on ember.js, and using ember.js with Rails.

I like the idea of Rails providing the API, and ember.js handling the UI, but I was wondering how to plug User authentication.

Eg, say I am using devise, how do I use it with ember.js?

Any pointers would be much appreciated.

zsquare
  • 9,916
  • 6
  • 53
  • 87

4 Answers4

15

Update 2

Check out this new github example app that uses a hybrid rails view + devise-variable-scoped ember app that runs on ember 1.0.rc.1 + ember-data rev 11 and persists has_many relationships.

Check out this token authentication project that isn't quite ready but shows a lot of promise. This could be great for mobile client apis that authenticate users client-side, as opposed to simple web-based rails view devise auth.

Both provide for interesting and debatable use cases. I look forward to seeing where this goes in the future.

Update 1

I added super basic user auth using Devise to Dgeb's ember-data example app here: https://github.com/dgeb/ember_data_example/pull/17 . I used Rails controllers for auth creation and scoping, adding current_user user_id's to the creation of new Contact records, while feeding scoped AMS data to the ember app through current_user.id.


I've had this very same question for a while and the only open-source ember example app I've been able to find that has user authentication and uses rails is https://github.com/carvil/dash-it. He uses a Rails-Devise login view that passes along current_user info to an ember.js app once the user is logged in.

I know many other people use Devise token_authenticatable to completely remove Rails views and authenticate users in Ember apps strictly through tokens. This is probably the best way to go, but I haven't tried implementing it in an app myself yet, and I haven't found any open source apps that go this route.

Tom Dale, one of the lead developers of Ember, says that they don't have a "favored nation" status yet for any specific user auth solution, so until they either officially support a specific solution or build one into the framework, I'm assuming Devise tokens are the best way to go.

Hopefully someone that has more Ember/user auth experience than me can chime in and demonstrate a better solution than https://github.com/carvil/dash-it, using Ember's state machine and routing in combination with token-based user auth.

Jack Johnson
  • 704
  • 1
  • 6
  • 14
  • Hey! Thanks for the response. I was also leaning towards a token based authentication system, to keep things truly restfull. What Ive found is that most examples online only talk about trivial use cases. – zsquare May 30 '12 at 08:55
  • 5
    That is probably Ember's biggest weakness - there aren't a lot of open source examples of best practices yet, as the core team is working hard to get the framework to a 1.0 release. A huge strength, however is the community, which is full of brilliant developers. I'm hoping one of those brilliant developers will spare a bit of time and demonstrate a "best practices" way to handle user auth with ember and rails tokens here on StackOverflow. Hopefully soon you'll get a better response than I can give right now! – Jack Johnson May 30 '12 at 09:05
  • Lets see. I tried asking on the same thing on irc, but got no response there. – zsquare May 30 '12 at 09:06
  • Most of the core guys on emberjs IRC are probably on west coast U.S. PST, meaning they're probably asleep right now. – Jack Johnson May 30 '12 at 09:11
  • Ah! Ok! Il probably ping them sometime later. – zsquare May 30 '12 at 09:22
  • Although the availability of ember.js best practices has improved since, it still lacks compared to backbone.js. A workaround is to search for "backbone.js authentication" or "backbone.js devise". Best practices in backbone should apply to ember – Panagiotis Panagi Aug 20 '12 at 14:34
4

Check ember-auth it handles token authentication for ember.js and there is also a demo and tutorial for rails / devise / ember. ember-auth is the best approach I've seen so far to authenticate an ember.js / rails app.

So far I have implemented a basic Ember application based on Ember-auth with Devise token authentication and example Oauth for Google and LinkedIn that can be found here and is live here: https://starter-app.herokuapp.com

joscas
  • 7,474
  • 5
  • 39
  • 59
2

I am working on a project with ember.js authentication on ROR platform. Check the below project link on github.

https://github.com/karthikkck/rails-emberjs.git

kck
  • 108
  • 1
  • 7
  • You might want to consider Active_Model_Serializers for JSON instead of JBuilder, Ember-data (will eventually be merged into Ember core) instead of ember-rest and using Ember's new layout and routing (in core on master branch on github) instead of Ghempton's external gems. – Jack Johnson Jun 20 '12 at 18:42
  • This video explains the Ember core team's future gameplan in detail (including using Active_Model_Serializers): http://confreaks.com/videos/907-railsconf2012-rails-the-next-five-years . Yehuda is a core Rails, Ember and Jquery team member and this is the Ember team's plan for Ember and Rails going forward. – Jack Johnson Jun 20 '12 at 18:43
1

One option (not always ideal in all situations) is to initialize the Ember app in a view that requires an active session in Devise to render.

I often do this by keeping the ember app's js/css manifest out of the load path except in a view that sits under an authenticated controller in the Rails app.

You can also protect any data from loading in the Ember app by putting Devise authentication checks on the Rails controllers that populate your data.

sirvine
  • 1,045
  • 11
  • 12