API Authentication using Devise (Ruby on Rails)

前端 未结 1 482
遇见更好的自我
遇见更好的自我 2021-01-31 12:39

I\'m trying to add Authentication via json in my project using devise configuration :token_authenticatable.

I have the working sessions_controller#create code which was

相关标签:
1条回答
  • 2021-01-31 12:59

    You want your SessionsController#create method to look something like this:

    class Users::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 do
            respond_with resource, location: redirect_location(resource_name, resource)
          end
          format.json do
            render json: { response: 'ok', auth_token: current_user.authentication_token }.to_json, status: :ok
          end
        end
      end
    end 
    

    ...and make sure you've configured devise to use the token authentication key your clients will pass along.

    0 讨论(0)
提交回复
热议问题