Using Devise 1.3 to authenticate JSON login requests

前端 未结 3 2163
清歌不尽
清歌不尽 2020-12-09 22:44

I am new to rails. I am trying to write an API for a mobile application which authenticates using JSON. I want to use Devise for the authentication. I believe Devise >1.3 ad

3条回答
  •  执念已碎
    2020-12-09 23:31

    The Jesse Howarth's solution will break logging with browser when using html format responses. If you want to make both JSON and HTML to work try someting like this:

    class 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 { respond_with resource, :location => after_sign_in_path_for(resource) }  
          format.json {  
             return render :json => {  :success => true, 
               :user => resource
             } 
          }  
        end
      end
    end
    

    And don't forget to change your routes to use this controller

提交回复
热议问题