So, I know this has been done a fair bit already, but I wanted to run this idea through the Ember community for another set of eyes.
I’m writing a fairly simple emberjs app which looks kind of like a specialised newsfeed. Events happen, my users need to know about them. There’s fun stuff that makes it Different, but that’s not important here. There’s an API endpoint run as a ruby/rails server which serves up stuff like /api/users/:user_id/feed/:feed_id sort of stuff.
I’m handling authentication using omniauth, and since I’m writing this app for an internal use and my foundation uses google apps (free for nonprofits, whee), I can just hardcode in “just use google oauth2” and be done with it - no need to store passwords or anything. Easy.
I do the standard thing and store authentication data in ruby’s session store, and as I understand it the cookie I hand back is actually just a session identifier and it’s all secure and stuff (provided I make a new secret key), and while it’s vulnerable to normal session problems (fixation, hijacking), this isn’t new or interesting. Same as always.
The problem I’m having is actually communicating which user the currently-active user actually is. If they want to view their feed or edit their profile, they need to go to the /api/users/N for their personal N. Any other number in that path returns a 401 from the server side (session checking). At first, I put an additional endpoint in my api, serving the current session user as /api/users/current, but all the major persistence frameworks had a real problem with that- they really want the object’s API path to contain the actual id (so I can’t serve {name: eric id:2 email:eric@awesome.com} as json response on /api/users/current and have the resultant model get the id field to be 2 - the ID on the model ends up as “current”, somewhat understandably, but also frustratingly.
Rather than wrestle with the persistence libraries, I decided to just hand the user_id value off to the client in a cookie. So now, my “are you logged in” checks (which live in AuthedRoute that all my authed routes extend) look for the existence of that cookie and set a current_user on their controller.
The problem that I’m worried about is it feels a tiny bit less secure passing user_id in the clear. It probably isn’t an issue - all my api endpoints have before_filters on them requiring logins, and I’m not worried about non-logged-in users seeing my empty templates (they’ll redirect to the please-log-in view if there’s no current_user), but it has been a loooong time since I’ve worried about actually implementing authentication in the client side (this project is my first real foray into webapp dev since ye olde cgi-bin on apache days in the late 1990s, no joke), so I wanted to run this idea by the community before I made some kind of Huge Mistake.