title: No current_user for Rails API using DeviseTokenAuth and Ionic
body: <p>I'm trying to access the Devise <code>:authenticate_user!</code> method in my API. I've followed the <a href="https://github.com/lynndylanhurley/devise_token_auth#concerns" rel="nofollow">Devise Token Auth gem</a>, but I cannot access the <code>current_user</code> variable/helper method within my API controller when accessing through Ionic with json.</p>
  
  <p>User.rb model:
  </p>
  
  <pre><code>class User &lt; ActiveRecord::Base
  include DeviseInvitable::Inviter
  devise :database_authenticatable, :recoverable, :rememberable,
       :trackable, :validatable, :omniauthable, :timeoutable,
       :invitable, :invite_for =&gt; 2.weeks
  include DeviseTokenAuth::Concerns::User
  </code></pre>
  
  <p>Application.rb
  </p>
  
  <pre><code>class Application &lt; Rails::Application
  config.middleware.insert_before 0, "Rack::Cors" do
    allow do
      origins "*"
      resource "*",
        headers: :any,
        expose: ['access-token', 'expiry', 'token-type', 'uid', 'client'],
        methods: [:get, :put, :post, :delete, :options]
    end
  end
  </code></pre>
  
  <p>Api Controller:
  </p>
  
  <pre><code>module Api
    class MobileController &lt; ApplicationController
      include DeviseTokenAuth::Concerns::SetUserByToken
      respond_to :json
      before_filter :authenticate_user!
  
      def facility_contacts
        @facility = current_user.facility
      end
    end
  end
  </code></pre>
  
  <p>Devise User::SessionsController
  </p>
  
  <pre><code>class User::SessionsController &lt; Devise::SessionsController
    include DeviseTokenAuth::Concerns::SetUserByToken
  
    respond_to :json, :html
  
    def create
      sign_out(:user) if current_user
      self.resource = warden.authenticate!(auth_options)
      set_flash_message(:notice, :signed_in) if is_flashing_format?
      sign_in(resource_name, resource)
      yield resource if block_given?
      respond_with resource, location: after_sign_in_path_for(resource)
    end
  end
  </code></pre>
  
  <p>Ionic app service and controller:
  </p>
  
  <pre><code>.factory('UserSession', function($resource) {
    return $resource("http://localhost:3000/users/sign_in.json")
  })
  
  .controller('LoginCtrl', function($scope, $location, UserSession, $ionicPopup, $rootScope) {
    $scope.data = {}
    $scope.login = function() {
    var user_session = new UserSession({ user: $scope.data })
    user_session.$save(
      function(data) {
        window.localStorage['userId'] = data.id
        window.localStorage['userName'] = data.name
        $location.path('/tab/daily_updates')
      },
      function(err) {
        var error = err['data']['error'] || err.data.join('.')
        var confirmPopup = $ionicPopup.alert({
          title: 'An error occured',
          template: error
        })
      }
    )
  }
  })
  </code></pre>