Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login with Devise via jQuery Mobile

We have a working app using Devise for authentication (email, password).

To add the mobile interface we're using jQuery mobile — everything works fine, except the login via Devise won't behave.

After hitting “login” Rails processes the request correctly but all we get is a MissingTemplate (devise/sessions/create) error.


Current settings:

config.http_authenticatable_on_hxr = false

config.navigational_formats = [:"*/*", "*/*", :html, :mobile]

We're inspired by Railscast #199 (hence :mobile): http://railscasts.com/episodes/199-mobile-devices

like image 608
schrader Avatar asked Apr 26 '11 18:04

schrader


2 Answers

Try the last step in this guide How To: Make Devise work with other formats like mobile, iphone and ipad (Rails specific).

Remember to use :to_mobile instead of :to_ios, so:

ActionController::Responder.class_eval do
  alias :to_mobile :to_html
end

in a new initializer file in config/initializers

like image 104
OGT Avatar answered Nov 12 '22 21:11

OGT


Overwriting Devise's SessionsController works. I took the controller's code from github and added the redirect_to for mobile devices:

app/controllers/sessions_controller.rb:

class SessionsController < Devise::SessionsController
  def create
    resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
    set_flash_message(:notice, :signed_in) if is_navigational_format?
    sign_in(resource_name, resource)
    if mobile_device?
      redirect_to wherever_you_want
    else
      respond_with resource, :location => redirect_location(resource_name, resource)
    end
  end
end

And in routes.rb:

devise_for :users, :path => '/', :controllers => { :sessions => "sessions" }

Don't forget to create app/views/sessions/ templates.

like image 41
csch Avatar answered Nov 12 '22 23:11

csch