I'm probably a little late to this party, but I thought I'd document
the steps I took to get this working in case anyone else is also
wondering about this issue.
So first off, thanks to Andre for pointing out that this change
already exists in Devise master; go ahead and use that branch in your
app. Next, in your devise initializer, set the following config:
# Set this to true to use Basic Auth for AJAX requests. True by
default.
config.http_authenticatable_on_xhr = false # Don't want basic auth
(we want redirect)
...
config.navigational_formats = [:html, :js]
I basically got this from looking at the code, especially
failure_app.rb. The respond method shows:
def respond
if http_auth?
http_auth
elsif warden_options[:recall]
recall
else
redirect
end
end
So if we want a redirect, we just have to make http_auth? return false
(ignoring warden_options[:recall] for now). That method shows:
def http_auth?
if request.xhr?
Devise.http_authenticatable_on_xhr
else
!(request.format && Devise.navigational_formats.include?
(request.format.to_sym))
end
end
That's how I came about the http_authenticatable_on_xhr setting.
Finally, take a look at redirect_url
def redirect_url
request_format = request.format.to_sym
if request_format == :html
send(:"new_#{scope}_session_path")
else
send(:"new_#{scope}_session_path", :format => request_format)
end
end
So this will basically send :js for new_user_session_path (if your
scope is user). Now, it should be pretty obvious that the file we need
to create is views/devise/sessions/new.js.erb (replace "devise" with
your scope if you have moved the views out into separate scopes).
In my new.js.erb, all I did was set the window.location to the sign in
path.
Hope this helps someone,
Mohan
On Jan 24, 7:46 am, lfat <
larsf2...@gmail.com> wrote:
> Thanks Andre, that helped a great deal but now i'm stuck catching the
> 401 and doing something with it.
>
> Can you tell me where should i put this login.js.erb?
>
> Thanks
> Lars
>
> On Jan 16, 8:30 pm, Andre Meij <
ahm...@gmail.com> wrote:
>
>
>
> > In Devise master the problem is fixed: the login.js.erb will be rendered instead of sending authorization headers. in the login.js one can put some javascript to deal with the situation // call some javascript function already available on the page.
> > For this to work :js must be added to the navigational_formats
>
> > Regards,
> > Andre
>
> > On Jan 16, 2011, at 8:36 PM, Piotr Gęga wrote:
>
> > > One of the solutions is to use 403 instead of 401 and render empty resp. instead of doing redirect
>
> > > render :nothing => true, :status => 403
>
> > > then, ie. you can handle 403 inajax-based callback. In Jquery it's Jquery.ajaxError callback.
>
> > > On Sun, Jan 16, 2011 at 5:33 PM, Joey <
j...@aghion.com> wrote:
> > > I had been hoping that devise could send the 401 response (signaling
> > > authentication failure) without sending the WWW-Authenticate header
> > > (causing the browser prompt). In fact, this is possible, if I
> > > customize the http_auth method of devise's FailureApp. However, I've
> > > since realized that HTTP actually requires that WWW-Authenticate
> > > header with every 401 response, so that seems to be the wrong road to
> > > go down.
>
> > > One requirement I have is that the client-side code can choose how to
> > > handle the auth failure as appropriate in different contexts. So,
> > > always returning a "window.location = ..."-style redirect doesn't
> > > quite work (e.g., when responding to a browser extension's request).
> > > Instead, I'm currently returning a custom status code in the failure
> > > case, as described here:
> > >
http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-re...
>
> > > This is an ugly solution and I'm unsatisfied with it, but it's
> > > certainly not a Devise bug. I'm just surprised there aren't better
> > > conventions for this.
>
> > > On Jan 14, 7:30 am, AH Meij <
ahm...@gmail.com> wrote:
> > > > Not sure what happened to my previous message however, I had the same
> > > > problem (http auth dialogs for jqueryajaxrequests) and for the