Edit: I’ve solved the problem myself. If anyone has a better or more elegant solution, please post it here and I’ll select as the answer.
I’, using jQuery Mobile and mobile_fu to set up a mobile view for a rails3 application.
Devise is used for authentication, and it works perfectly in the desktop view.
My problem is that after a successfully login in the mobile view, the application redirects again to the sign_in form when it should redirect to the root page. The sign in is correct, and if i just reload the page, then the redirection to root is correctly made. The sequence is as follows:
I enter the root page URL in the mobile device
As the user is not authenticated, devise redirects to the sign in page
I enter the credentials and hit the login button
The login is performed correctly, but the application redirects to the sign in page again
Hit reload in the mobile device, and then the application redirects to the root page with the user correctly authenticated
I’ve followed this instructions to set up devise with mobile fu, but using a ‘mobile’ format instead of ‘iOS’ format:
https://github.com/plataformatec/devise/wiki/How-To%3a-Make-Devise-work-with-other-formats-like-mobile,-iphone-and-ipad-%28Rails-specific%29
Any help will be appreciated, i’ve been working on this issue a couple of days and it’s starting to drive me crazy :S
Thanks in advance!
I attach the request and response data both for mobile and desktop requests and responses when signing in:
Desktop login
Request
POST /users/sign_in HTTP/1.1 Host: localhost:3000 User-Agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101
Firefox/7.0.1 Accept:
text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Accept-Language: es-es,es;q=0.8,en-us;q=0.5,en;q=0.3 Accept-Encoding:
gzip, deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive Referer: http://localhost:3000/users/sign_in
Cookie:
_PMG_session=BAh7CkkiCmZsYXNoBjoGRUZJQzolQWN0aW9uRGlzcGF0Y2g6OkZsYXNoOjpGbGFzaEhhc2h7BjoKYWxlcnRJIkJOZWNlc2l0YXMgaW5pY2lhciBzZXNpw7NuIGNvbiB0dSBjdWVudGEgcGFyYSBwb2RlciBjb250aW51YXIuBjsAVAY6CkB1c2VkbzoIU2V0BjoKQGhhc2h7BjsHVEkiD3Nlc3Npb25faWQGOwBGIiViZjlkZDc4NzczMmFmMGViNjcyOWRkNzAxNWJhMzQ5YkkiEG1vYmlsZV92aWV3BjsARkZJIhN1c2VyX3JldHVybl90bwY7AEZJIgYvBjsARkkiEF9jc3JmX3Rva2VuBjsARkkiMWEyZW9oamFKT2tsWEZrbzZxc3lNenRhWTVEUk5XaHVUdm9iV3J3aFhFK1E9BjsARg%3D%3D–6623519d539757216a03d212a4e20647e8a5029c;
jstree_open=%231%2C%234%2C%235; jstree_load=Response
HTTP/1.1 302 Found Location: http://localhost:3000/ Content-Type:
text/html; charset=utf-8 Cache-Control: no-cache X-UA-Compatible:
IE=Edge X-Runtime: 1.650766 Server: WEBrick/1.3.1
(Ruby/1.9.2/2011-02-18) Date: Mon, 31 Oct 2011 08:58:24 GMT
Content-Length: 88 Connection: Keep-Alive Set-Cookie:
_PMG_session=BAh7CkkiCmZsYXNoBjoGRUZJQzolQWN0aW9uRGlzcGF0Y2g6OkZsYXNoOjpGbGFzaEhhc2h7BjoLbm90aWNlSSIoSGFzIGluaWNpYWRvIHNlc2nDs24gY29ycmVjdGFtZW50ZS4GOwBUBjoKQHVzZWRvOghTZXQGOgpAaGFzaHsASSIQbW9iaWxlX3ZpZXcGOwBGRkkiEF9jc3JmX3Rva2VuBjsARkkiMWEyZW9oamFKT2tsWEZrbzZxc3lNenRhWTVEUk5XaHVUdm9iV3J3aFhFK1E9BjsARkkiGXdhcmRlbi51c2VyLnVzZXIua2V5BjsAVFsISSIJVXNlcgY7AEZbBmkGSSIiJDJhJDEwJEZQL20vZ29KU2FGUFZ0bmRzb0VZeS4GOwBUSSIPc2Vzc2lvbl9pZAY7AEYiJTA1NzI5YjFjOTMyZDdlMjk3YjEwYWQ5NWU3ZDJkZjYx–889ba65c483c3757b6aa47ffc2e187ac27a04dbe;
path=/; HttpOnlyMobile login
Request
POST /users/sign_in HTTP/1.1
Host: localhost:3000
User-Agent: Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X;
en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341
Safari/528.16Accept: text/html, /; q=0.01
Accept-Language: es-es,es;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://localhost:3000/users/sign_in.mobile
Content-Length: 195
Cookie: jstree_open=%231%2C%234%2C%235; jstree_load=;
_PMG_session=BAh7CkkiCmZsYXNoBjoGRUZJQzolQWN0aW9uRGlzcGF0Y2g6OkZsYXNoOjpGbGFzaEhhc2h7BjoKYWxlcnRJIkJOZWNlc2l0YXMgaW5pY2lhciBzZXNpw7NuIGNvbiB0dSBjdWVudGEgcGFyYSBwb2RlciBjb250aW51YXIuBjsAVAY6CkB1c2VkbzoIU2V0BjoKQGhhc2h7BjsHVEkiD3Nlc3Npb25faWQGOwBGIiUwMjY0ZGFkODY2MGY3YmE5ZWY4NmQzYmRmODUyNDUxOEkiEG1vYmlsZV92aWV3BjsARlRJIhN1c2VyX3JldHVybl90bwY7AEZJIgYvBjsARkkiEF9jc3JmX3Rva2VuBjsARkkiMTN5am1kaUhZcWpEZTAyclNNZ05ZNU5aZjA4bG5mamxCZHR6UmlVVHpkNFk9BjsARg%3D%3D–a6c9dbd6b27ac1b43193b4cba901b8862046dacf
Pragma: no-cache Cache-Control: no-cacheResponse
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Etag: “22b69d3da00b31c705b66d95aa8a32de”
Cache-Control: max-age=0, private, must-revalidate
X-UA-Compatible: IE=Edge
X-Runtime: 1.317610
Server: WEBrick/1.3.1 (Ruby/1.9.2/2011-02-18)
Date: Mon, 31 Oct 2011 09:06:47 GMT
Content-Length: 2915
Connection: Keep-Alive
Set-Cookie:
_PMG_session=BAh7C0kiCmZsYXNoBjoGRUZJQzolQWN0aW9uRGlzcGF0Y2g6OkZsYXNoOjpGbGFzaEhhc2h7BjoLbm90aWNlSSIoSGFzIGluaWNpYWRvIHNlc2nDs24gY29ycmVjdGFtZW50ZS4GOwBUBjoKQHVzZWRvOghTZXQGOgpAaGFzaHsASSIQbW9iaWxlX3ZpZXcGOwBGVEkiEF9jc3JmX3Rva2VuBjsARkkiMTN5am1kaUhZcWpEZTAyclNNZ05ZNU5aZjA4bG5mamxCZHR6UmlVVHpkNFk9BjsARkkiCWxpbmUGOwBGSSIGNQY7AFRJIhl3YXJkZW4udXNlci51c2VyLmtleQY7AFRbCEkiCVVzZXIGOwBGWwZpBkkiIiQyYSQxMCRGUC9tL2dvSlNhRlBWdG5kc29FWXkuBjsAVEkiD3Nlc3Npb25faWQGOwBGIiVhYWNkYzNlNjNhODU2ZWVjNmY1YmYzODEwNTk3NDA5Zg%3D%3D–31e22835ee0e428f80e0cf3f3ce5bd9fc8134d20;
path=/; HttpOnly
I’ve managed to solve the problem. Maybe I’ve should asked before to get the inspiration running 🙂
Apparently, Devise was not able to redirect correctly when the request format was not HTML. Unfortunately I relied on the format to serve different templates for a mobile device using mobile_fu, and forcing the request format as devise wiki suggested order to get it running right with jquery mobile.
The solution I’ve found is an ugly one:
When the user makes the POST request to the devise controller -when is trying to authenticate- I force the html format instead of the mobile one. That way Devise is able to make the redirection correctly.
Code for ApplicationController class:
Hope this can help anyone who runs into the same problem.
More elegant solutions are welcome!