Extending Devise SessionsController to authenticate using JSON

后端 未结 7 1297
面向向阳花
面向向阳花 2020-12-04 05:14

I am trying to build a rails API for an iphone app. Devise works fine for logins through the web interface but I need to be able to create and destroy sessions using REST AP

相关标签:
7条回答
  • 2020-12-04 06:12

    This is what finally worked.

    class Api::V1::SessionsController < Devise::SessionsController  
      def create  
        respond_to do |format|  
          format.html { super }  
          format.json {  
            warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")  
            render :status => 200, :json => { :error => "Success" }  
          }  
        end  
      end  
      def destroy  
        super  
      end  
    end  
    

    Also change routes.rb, remember the order is important.

    devise_for :users, :controllers => { :sessions => "api/v1/sessions" }
    devise_scope :user do
      namespace :api do
        namespace :v1 do
          resources :sessions, :only => [:create, :destroy]
        end
      end
    end
    
    resources :users
    
    0 讨论(0)
提交回复
热议问题