heartcombo / devise

Flexible authentication solution for Rails with Warden.
http://blog.plataformatec.com.br/tag/devise/
MIT License
23.97k stars 5.55k forks source link

Can you give a tutorial to use devise login using ajax? #1342

Closed naveedtariq closed 13 years ago

naveedtariq commented 13 years ago

I have been trying to log the users in through ajax. I have tried to follow this article to attain this:

http://jessehowarth.com/2011/04/27/ajax-login-with-devise.

It all goes well except that now if I submit the form empty it gives me the error that is "'You need to sign in or sign up before continuing.'" instead of saying "Email can't be blank" and "Password can't be blank".

If youcan add a HOW TO that would great.

Thanks

josevalim commented 13 years ago

On latest Devise, 1.4.6, you don't need to create your own controller. You should just copy the views to your application, configure them to use remote and then everything should work. If you still can't make it work, I suggest you to send an e-mail to the mailing and see if someone has an example to share.

rohineematale commented 11 years ago

followed the documentation

like I did

config.http_authenticatable_on_xhr = true
config.navigational_formats = [ "*/*", :html, :js]

Create a create.js.erb in app/views/devise/sessions/

Yet I see my ajax response getting 302 as the response header

I believe that is because of this

  :location => after_sign_in_path_for(resource)

Now how can I not redirect for ajax request and have devise working with ajax login

am I missing something

my create,js.erb look as follow

<% if current_user %>
    alert("Sign in successfully");
<% else %>
    alert("Please Sign in");
<% end %>
ericpeters0n commented 11 years ago

Hmmm... Is it possible that the latest version of Devise is treating 'remote: true' (javascript) requests as html?

Here's what I'm seeing here:

Started GET "/users/sign_in" for 127.0.0.1 at 2013-03-25 18:22:59 -0700 Processing by Devise::SessionsController#new as JS Rendered devise/shared/_links.erb (0.4ms) Rendered devise/sessions/new.html.erb within layouts/application (7.8ms)

My sessions/new.js.erb is completely ignored.

Please advise.

williamweckl commented 10 years ago

My create.js.erb is beeing ignored too. Have you figured out?

oarod commented 7 years ago

This stackoverflow post helped me figure out the :js responses: http://stackoverflow.com/questions/22689877/devise-ajax-login-sessionscreate-only-render-create-js-erb-when-it-succeed/31713225#31713225