0

I've got a rails application which uses Devise for authentication and works fine in the browser. I want to configure it so that it will interact with an Android application I'm building. I'm trying to override the standard JSON response so that it returns the authentication token which I need to login.

This problem seems to be answered in this question and I'm trying to adapt it. Using Devise tokens to log in, is this built in?

I have specified the following config/initializers/devise.rb:

config.token_authentication_key = :auth_token

I've changed my routes file so that it looks like this: devise_for :managers, :controllers => { :sessions => "Managers/sessions" }

And then I've created a file sessions_controller.rb with the following:

class Managers::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)

respond_to do |format|
  format.html do
    respond_with resource, :location => redirect_location(resource_name, resource)
  end
  format.json do
    render :json => { :response => 'ok', :auth_token => current_user.authentication_token }.to_json, :status => :ok
  end
end
end
end 

However when I try to run the application I get the following error:

Routing Error uninitialized constant Managers

What's causing this error? Any suggestions for how to correct this so that I can still log in via JSON?

Thanks in advance for your help!

Antony

Community
  • 1
  • 1
TechnoTony
  • 1,587
  • 1
  • 15
  • 24

1 Answers1

0

this worked for me.

my routes

devise_for :users , :controllers => {:sessions => 'sessions'}

and then in sessions_controller.rb

 class SessionsController < Devise::SessionsController

Do not forget to initialise your token or reset it at some point if you want to have a unique token for a session (current_user.reset_authentication_token!)

  • to be precise (just tried out in order to be less intrusive) you could keep your code copying the file in controllers/managers/sessions_controller.rb. I would change the routes to lower case (controllers => { :sessions => "managers/sessions" } ) –  Nov 28 '11 at 15:07