title: Return JWT token when user signs in rails
body: <p>Having an issue with getting JWT token using <code>devise gem</code> and <code>devise-jwt gem</code>. This is how my confirmation looks like. </p>
  
  <p><strong>devise.rb</strong></p>
  
  <pre><code>  Devise.setup do |config|
  
      config.jwt do |jwt|
        jwt.secret =  SECRETS.devise_jwt_secret_key
        jwt.dispatch_requests = [ ['POST', %r{^/authentication_tokens/create$}] ]
      end
  end 
  </code></pre>
  
  <p><strong>user.rb</strong></p>
  
  <pre><code>class User &lt; ApplicationRecord
  
      devise :database_authenticatable, :registerable,
             :recoverable, :rememberable, :trackable, :validatable,
             :jwt_authenticatable, jwt_revocation_strategy: Devise::JWT::RevocationStrategies::Null
  
    end
  </code></pre>
  
  <p><strong>authentication_tokens_controller.rb</strong></p>
  
  <pre><code>class Api::V1::AuthenticationTokensController &lt; Devise::SessionsController
    include Devise::Controllers::Helpers
    skip_before_action :verify_authenticity_token 
  
    prepend_before_action :require_no_authentication, only: [:create]
  
    before_action :rewrite_param_names, only: [:create]
  
    def new
      render json: { response: "Authentication required" }, status: 401
    end
  
    def create
      self.resource = warden.authenticate!(auth_options)
      sign_in(resource_name, resource)
      yield resource if block_given?
       render json: {success: true, jwt: current_token, response: "Authentication successful" }
    end
  
    private
  
    def rewrite_param_names
      request.params[:user] = {email: request.params[:email], password: request.params[:password]}
    end
  
    def current_token
      request.env['warden-jwt_auth.token']
    end
  
  end
  </code></pre>
  
  <p><strong>routes.rb</strong></p>
  
  <pre><code>   get 'home#secret'
     devise_for :users
     resources :tasks
     #other routes for the website removed for brevity
  
    namespace :api, defaults: { format: :json } do
      namespace :v1 do
        resources :users
        devise_scope :user do
          post '/authentication_tokens/create', to: "authentication_tokens#create"
        end
      end
    end
  </code></pre>
  
  <p>For some reason <code>request.env['warden-jwt_auth.token']</code> returns <code>null</code> all the time, however, the user is authenticated.
  Is there anything that I need to add to get the JWT token when a user signs in?</p>
  
  <p><strong>Update - routes and namespacing</strong></p>
  
  <p>After days of debugging, I believe I have found the source of my problem.  My app has a frontend which uses normal routes. The code above doesn't work however if I do something like the code below. All is good.</p>
  
  <pre><code>  scope :api, defaults: {format: :json} do
      devise_for :users, controllers: {sessions: 'v1/authentication_tokens'}
    end
  </code></pre>
  
  <p>Is there a way of namespacing the <code>devise_for</code> for me API even though it has been used above for the website?</p>