Hi,
I have been following the instructions on this board and OS board on
configuring Devise to work with AJAX. It has been a difficult
journey, but I think I am close. For anyone who is trying this, here
is a short summary of steps I took (after Devise is working in normal
http mode)
S1. Make a custom sessions controller (
https://gist.github.com/418243)
and put it in app/controllers/users/sessions_controller.rb (my model
happens to be users, yours can be different)
S2. Edit config/initializers/devise.rb to contain the following lines:
config.http_authenticatable = false
config.http_authenticatable_on_xhr = false
config.navigational_formats = [:"*/*",:html] #
tried :js, :JS, :json, no effect
S3. Edit config/routes.rb to contain the route below:
devise_for :users, :controllers => { :sessions => "users/sessions" }
S4. In html.erb, use POST to create session:
var url = '/users/sign_in.js'
$.ajax({ type: 'POST', url: url,
data: { "remote" : true, "commit" : "Sign in", "utf8" : "✓", "user" :
{
"remember_me" : v_login_remember,
"password" : v_login_password,
"email" : v_login_email},},
success: function(json){ // do something here}
});
My observations are:
O1. resource = warden.authenticate!(:scope => resource_name, :recall
=> "failure") does not call "failure", as others have pointed out ()
O2. warden.authenticate! redirects after successful sign_in (if this
is true, could someone show me how to disable this redirect? If this
is not true, could someone point out where the redirect is taking
place?) Redirect happens even if sign_in_and_redirect function is
removed from
https://gist.github.com/418243
O3. Actually, if ajax is the goal, instead of
https://gist.github.com/418243,
all you need in the sessions controller is 4 lines.
class Users::SessionsController <
Devise::SessionsController
def create
resource = warden.authenticate!(:scope => resource_name)
render :json => { :success => true }
end
end
O4. I still get redirects after successful sign_in, unsuccessful
sign_in, and sign_out (destroy)
O5.
https://gist.github.com/589639 describes a fix for login redirect,
but Im lost on where to put this file. Has anyone tried this? Love
to hear some details.
O6. If anyone has any insights on how to disable redirects in Devise,
I would love to hear more. It really defeats the point of using ajax
if my page is going to reload after any authentication action.
O7. For dealing with browser http authentication pop-up, this link has
good resources:
http://groups.google.com/group/plataformatec-devise/browse_thread/thread/30553801dc60076a/ae27657844d6e505?lnk=raot
The question is: How do I disable any devise/warden redirect after
login logout signup, actions, so that I can have only ajax do the
work.
Rails 3.0.3, devise 1.1.5 master branch, warden 1.0.3/1.0.2