Note that there are some explanatory texts on larger screens.

plurals
  1. PODevise: Have multiple controllers handle user sessions
    primarykey
    data
    text
    <p>I am running devise 1.3.4 with rails 3.0.7. I have two ways users may sign in: using the web app, and using a mobile web app (via a JSON API call). The first way is handled perfectly by the default devise sessions controller. The API-call method of authentication needs to be in a controller that extends my <code>Api::BaseController</code>. So, I wrote this second controller like this:</p> <pre><code>class Api::UserSessionsController &lt; Api::BaseController … def create user = warden.authenticate(:scope =&gt; :user) if user sign_in(:user, user) else # Do some error handling end end end </code></pre> <p>Attempts to login via this method fail due to the <code>valid_controller?</code> method in <code>Devise::Strategies::Authenticatable</code>. Because I have left the default controller (<code>devise/sessions</code>) as the mapped controller for users, it does not allow authentications from my custom controller. </p> <p>I would like to roll my custom functionality into my own subclass of <code>Devise::SessionsController</code>, but I <strong>need</strong> the API sessions controller to extend the <code>API::BaseController</code>, so I can't extend <code>Devise::SessionsController</code> as well. I don't want to place the working, default-behavior web-app authentication methods in the API controller, especially because this would require copying them from the devise controller.</p> <p>Any suggestions? Is there some config I'm missing that allows multiple controllers to handle sessions? the <code>valid_controller?</code> method does an <code>==</code> comparison, not <code>.include?</code>, so I don't see how that would work.</p> <p><strong>UPDATE</strong></p> <p>This is an awful temporary workaround. I don't like it, so I'm not posting it as an answer, but I thought it might offer food-for-thought to all you answerer-types:</p> <p>In the top of my create method, I could override what Devise expects to be the sessions controller.</p> <pre><code>Devise.mappings[:user].controllers[:sessions] = params[:controller] </code></pre> <p>This is working around Devise's intended functionality (requiring a single, specific controller to do session creation) so I don't want to keep it. I wonder if this constraint is a security measure or just a convention -- if it is for security, this is presumably quite bad.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload