TheJKFever
TheJKFever

Reputation: 705

Devise with different parent class

I'm using devise in my rails app, and I have a html version at '/...' and an api at '/api/v#/...' The implementation of devise controllers should be different at '/sign_in' and '/api/v1/sign_in' etc. Also I have an ApiController that all my controllers within the api namespace inherit from.

Basically I need two different sets of devise in my application, each with a different parent class.

I was able to generate the controllers in the api namespace and change the parent of devise to be the ApiController, but that messed up the regular site. That won't work.

I could make a module for my ApiController and then include it in each devise controller... but that seems smelly.

There's only a few features of devise I need within the api so maybe I could just implement them raw without devise. But then I'd lose confirmation functionality and other things.

What do I do?

Let me know what if any code you would like to see.

Upvotes: 1

Views: 901

Answers (1)

max
max

Reputation: 102036

You can specify what controllers to use in your routes:

devise_for :users # uses the normal devise controllers

namespace :api do
  devise_for :users, controllers: { sessions: 'api/sessions' }
end  

class Api::SessionsController < Devise::SessionsController
end

That said - creating your own controllers to handle authentication and sessions in an API with Warden (the lower level component of Devise) is pretty simple and is a lot cleaner than shoehorning Devise into doing token based auth or HMAC for example.

Upvotes: 0

Related Questions