Devise and Knock gem current_user

Tom Pinchen Source

I have an application that is using both Devise and Knock. It is using Devise to power the authentication for Active Admin and Knock gem is providing the authentication for my API's

The issue I have is that Knock can't seem to find current_user and I believe this is likely because I am using Devise in the same project.

I have the following setup:

Api Controller

class ApiController < ActionController::API
  include Knock::Authenticable
end

User Controller (for API not ActiveAdmin)

module Api
  module V1
    class UsersController < ApiController      
      before_action :set_user, only: [:show, :update, :destroy]

      # GET /users/1
      def show
        render json: @user
      end

      # POST /users
      def create        
        @user = User.new(user_params)               

        if @user.save
          render json: @user.id, status: :created
        else
          render json: @user.errors, status: :unprocessable_entity
        end
      end

      # PATCH/PUT /users/1
      def update
        if @user.update(user_params)
          render json: @user
        else
          render json: @user.errors, status: :unprocessable_entity
        end
      end

      # DELETE /users/1
      def destroy
        @user.destroy
      end

      private
      # Use callbacks to share common setup or constraints between actions.
      def set_user
        @user = User.find(params[:id])
      end

      # Only allow a trusted parameter "white list" through.
      def user_params
        params.require(:user).permit(:email, :password, :password_confirmation)
      end
    end
  end
end

Auth Controller

module Api
  module V1
    class AuthController < ApiController      
      def auth        
        render json: { status: 200, user: current_user }
      end
    end
  end
end

Current User in this Auth controller returns nothing however in another project I have, without devise, this will correctly return the user.

Is there a way to redefine what current_user is or assign it to something different for the purposes of using Knock?

ruby-on-railsrubydevisejwt

Answers

comments powered by Disqus