Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActionCable support? #44

Closed
xxSkyy opened this issue Mar 18, 2021 · 6 comments
Closed

ActionCable support? #44

xxSkyy opened this issue Mar 18, 2021 · 6 comments

Comments

@xxSkyy
Copy link

xxSkyy commented Mar 18, 2021

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
@Gokul595
Copy link
Owner

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

@xxSkyy
Copy link
Author

xxSkyy commented Mar 18, 2021

Somehow it helped, another error came

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

@Gokul595
Copy link
Owner

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

@xxSkyy
Copy link
Author

xxSkyy commented Mar 19, 2021

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?

@Gokul595
Copy link
Owner

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.

@xxSkyy xxSkyy closed this as completed Mar 22, 2021
@xxSkyy
Copy link
Author

xxSkyy commented Mar 24, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants