-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Action Cable and devise token auth #986
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
Comments
How about just checking the validity of the users' token based on their UID? Perhaps I'm misunderstanding how action cable is different from a controller action? |
As far as I know websockets/action cable do not let you specify headers. So we can't get the info like we can in controllers |
@jgoodall628 you will need to include the latest valid token info ( For example: # app/channels/application_cable/connection.rb
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user
def connect
self.current_user = find_verified_user
end
private
def find_verified_user
uid = params[:uid]
token = params[:token]
client_id = params[:client]
user = User.find_by_uid(uid)
if user && user.valid_token?(token, client_id)
user
else
reject_unauthorized_connection
end
end
end
end |
Instead of |
@juanmanuelramallo - thanks, that's exactly what I meant. does |
No, it doesn't 😕 |
Worth noting that you need to figure out how to update the token on your client (which for me is React Native) as the cable doesn't have headers to send you the next token/expiry. |
@wmlutz - You have to expose the headers by doing the following in
Then, after you make any kind of request you should store the new Anyways, for
|
@juanmanuelramallo Hmmmm. I'm not using Rack::Cors, but I may have to now. Thanks for the help. For reasons I haven't yet figured out, my tokens seem to loose validity randomly during the app's usage. AND I can't recreate the problem reliably so I'm having problems hunting this one down. My solution is probably going to be to set the next valid token every time a message is sent over actioncable. |
You shouldn't update manually the tokens during the socket connection, and in the case of react native, you have to send the id of the user through the headers @jgoodall628 Can we close the issue? |
@wmlutz I am having the same issue with my react native app, with my tokens randomly loosing validity. I also can't seem to reliably replicate and track down the issue. Did you ever figure it out? |
Here is how we use devise_token_auth with the application: module ApplicationCable
class Connection < ActionCable::Connection::Base
include ActionController::HttpAuthentication::Basic::ControllerMethods
include ActionController::HttpAuthentication::Token::ControllerMethods
identified_by :current_user
def connect
client = request.headers["client"]
uid = request.headers["uid"]
access_token = request.headers["access-token"]
self.current_user = find_verified_user access_token, uid, client
end
private
def find_verified_user token, uid, client_id # this checks whether a user is authenticated with devise
user = User.find_by email: uid
# http://www.rubydoc.info/gems/devise_token_auth/0.1.38/DeviseTokenAuth%2FConcerns%2FUser:valid_token%3F
if user && user.valid_token?(token, client_id)
user
else
reject_unauthorized_connection
end
end
end
end We simply send the Any idea ? |
Does anyone know how to authenticate action cable connections while using devise token auth? Currently have to authorize connections two different types of users as well customers and employees. I can't really find any info.
The text was updated successfully, but these errors were encountered: