Skip to content

Instantly share code, notes, and snippets.

@joefiorini
Created May 20, 2013 14:17
Show Gist options
  • Save joefiorini/5612528 to your computer and use it in GitHub Desktop.
Save joefiorini/5612528 to your computer and use it in GitHub Desktop.
// How you could do what you want in Ember
App.Router.map(function(){
this.resource("userSession", function(){
this.route("new", { path: "/sign-in" });
})
});
// However, any time you create a resource you have an extra layer of routing
// & a model that you may/may not want. In the case of sign-in, which doesn't
// really represent part of the UI, I'd consider just using a route instead.
App.Router.map(function(){
this.route("signIn", { path: "/sign-in" });
});
// Then in your controller you can set the current session on ApplicationController
App.SignInController = Ember.Controller.extend({
signIn: function(){
var session = UserSession.validate(this.get("username"), this.get("password")); // assuming you have some object with a session token or something
this.controllers("application").set("session", sesssion);
}
});
<label>
Email:
{{view Ember.TextField valueBinding="username"}}
</label>
<label>
Password:
{{view Ember.TextField valueBinding="password" type="password"}}
</label>
<button {{action signIn}}>Sign In</button>
@JoshReedSchramm
Copy link

Honestly I just had a really hard time finding an example online using devise w/ ember that didn't rely on logging in via a traditional webpage then passing you to the ember app. I wanted to make it all work in ember so that site was the best I found. Happy to look at alternatives.

In your approach I'm guessing the UserSession.validation method would make the request to the devise backend and if valid would return back the session token. How does this play with a "remember me?" style thing? I suppose it's just a cookie which would get set like normal on the web request so the auth call would just check if they are already logged in.

@joefiorini
Copy link
Author

I think the biggest reason you're having a hard time is that it's a security risk. There cannot be any chance the user can disable the auth check and start getting protected data. I'm sure you've already considered those implications though :)

You are correct on the purpose of UserSession.validate. You have to be careful with cookies & ajax requests; I'm not sure how browsers handle cookies in ajax responses but I seem to remember it being a no-no in some; don't remember which. You could also just use document.cookies to set it manually in Javascript.

@joefiorini
Copy link
Author

Also, not sure if you saw this but this looks like an interesting approach:

http://say26.com/using-rails-devise-with-ember-js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment