Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise RegistrationsController create action in API

I'm trying to create a Rails 3 app with an API. I use Devise as my authentication system. I would like to make it possible to sign up and later sign in through the API. And i'm not quite sure how I should do this. I have created separated controllers to manage the API, i.e. api/v1/account/1/projects.json. I'm currently trying to create a custom Devise::RegistrationsController for the API, but I'm not quite sure how to do this.

My create action currently looks like this:

class API::V1::RegistrationsController < Devise::RegistrationsController
  redpond_to :json, :xml

  def create
    build_resource
    if resource.save
      if resource.active_for_authentication?
        # Account is a nested resource for User, I want to 
        # create an associated account when the User signs up through the client, 
        # how should I do this?
        account = resource.accounts.build 
        # How can I get the sign_in to work?
        sign_in(resource_name, resource)
        # Probably wrong
        respond_with resource, :location => after_sign_up_path_for(resource)
      else
        # probably wrong
        expire_session_data_after_sign_in!
        respond_with resource, :location => after_inactive_sign_up_path_for(resource)
      end
    else
      # probably wrong, or?
      clean_up_passwords resource
    end
  end

Which probably is very incorrect as mentioned in my comments, so I wonder if anyone could give me any guidance on how to make it work. Should also mention that account is a nested resource for the user.

I also wonder how I should set up the login and logout functionality in the API with Devise.

Thanks!

like image 311
Anders Avatar asked Mar 10 '26 01:03

Anders


1 Answers

This other SO question may help you. It looks very similar. Pay special attention to the gist that's linked in the answer.

like image 106
smoak Avatar answered Mar 12 '26 14:03

smoak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!