jeudi 15 octobre 2015

rails devise auth token give 401

I'm trying to change my rails APP into an API.

So every respond as te be as JSON.

So I try to use Devise with an Authentification Token. Since is not supported, I used the gems 'devise_token_auth' & 'simple_token_authentication' but with no success.

there is my code for that :

routes.rb

devise_for :users, :controllers => {sessions: 'sessions', registrations: 'registrations'}

sessions_controller.rb

class SessionsController < Devise::SessionsController
respond_to :json
  clear_respond_to

  after_filter :set_csrf_header, only: [:new, :create]

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

  protected

  def set_csrf_header
     response.headers['X-CSRF-Token'] = form_authenticity_token
  end

end

The users registration is working properly, but the session login give me a 401 -> and after it redirect me on the session/new with the message 'You have to login before continue'

Aucun commentaire:

Enregistrer un commentaire