Gokul595 / api_guard

JWT authentication solution for Rails APIs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ActionCable support?

xxSkyy opened this issue · comments

commented

Hey, I'ma tryin to use this gem to auth ActionCable connection, but I cant manage it to work.
I'm kinda fresh user of rails websockets so I dont know if I'm doing all right but I think I do.
Also I'm doing own sort of auth as I made it with #38 to allow guest connections aswell.

on connection.rb I included helper that reads JWT (from cookies or params, doesnt matter here) and I pass it successfully in any possible way but after that I'm getting error.

There was an exception - NameError(undefined local variable or method decode_token' for #ApplicationCable::Connection:xxxxxxxxxxxx`

And I did try include ApiGuard and ApiGuard:JwAuth etc for helper, or for connection.rb, nothing worked

Example code of connection.rb that should work if decode_token would be defined

module ApplicationCable
  class Connection < ActionCable::Connection::Base
    # include ApiGuard::JwtAuth
    # include ApplicationHelper
    identified_by :current_user

    def connect
      # puts cookie_jwt_auth
      self.current_user = jwt_auth
    end

    private

    def jwt_auth
      @token = request.params[:token]
      @user = nil
      if @token
        begin
          user = decode_token
          puts user
          @user = User.find(user['user_id'])
        rescue Exception => e
          raise Exception, "JWT Token is not valid or user don't exist"
        end
      end
    end
  end
end

I haven't tried API Guard with ActionCable. Can you try including below modules and see how it works?

include ApiGuard::Resource
include ApiGuard::JwtAuth::JsonWebToken
include ApiGuard::JwtAuth::Authentication
include ApiGuard::JwtAuth::RefreshJwtToken
include ApiGuard::JwtAuth::BlacklistToken
commented

Somehow it helped, another error came

undefined local variable or method `controller_name' for #<ApplicationCable::Connection:0x000000012539efb0>

Can you try calling the method decode(@token) directly instead of decode_token?

commented

It works now! Thanks.
Anyway before I'll close it - are there some update option to avoid these changes or should I use it as it is now?

You can use it. I will try for the possibility to support action cable in API Guard and update here if I found anything. You can close this issue.

commented

Update:
I didnt notice before that with those includes ActionCable methods was overwritten and response wasnt hash with info , it was just JWT token I sent on handshake.

I figued out that its caused by some method from include ApiGuard::JwtAuth::JsonWebToken, so I closed it in class

  class Auth
      extend ApiGuard::JwtAuth::JsonWebToken
    end

and then to decode token used Auth.decode(@token) , after that so far it works.