일반적으로 Rails 5 API에서 쿠키는 지원되지 않습니다. 참조 : http://guides.rubyonrails.org/api_app.html#creating-a-new-application. access_token
, client
, uid
- 당신이 (devise_token_auth 보석과) 사이트에서 첫 번째 어딘가에서 일반적인 HTTP-위한 인증을 할 경우
는, 당신은 3 인증 헤더를 얻는다. 이러한 경우에
이러한 3 인증 헤더를 사용하여 (https://devcenter.heroku.com/articles/websocket-security#authentication-authorization 따라) 당신의 WebSocket을 연결 기본위한 인증을 사용할 수 있습니다
전화 (내가 Chrome Simple WebSocket Client 사용) :
ws://localhost:3000/cable/?access-token=ZigtvvcKK7B7rsF_20bGHg&client=TccPxBrirWOO9k5fK4l_NA&[email protected]
그런 과정 :
# Be sure to restart your server when you modify this file. Action Cable runs in an EventMachine loop that does not support auto reloading.
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user
def connect
params = request.query_parameters()
access_token = params["access-token"]
uid = params["uid"]
client = params["client"]
self.current_user = find_verified_user access_token, uid, client
logger.add_tags 'ActionCable', current_user.email
end
protected
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
일반적인 웹 소켓 인증과 비슷합니다. https://rubytutorial.io/actioncable-devise-authentication/
이러한 인증으로 충분할 수 있습니다. 나는 그것이 채널 구독에서 서버로 전송 모든 웹 소켓 메시지에 정식에 추가로 필요한 하지이라고 생각 : 서버에 의해 허용 모든 웹 소켓의 경우
http://guides.rubyonrails.org/action_cable_overview.html#server-side-components-connections
이 연결 객체가 인스턴스화됩니다. 이 객체 은에 생성 된 모든 채널 구독의 부모가됩니다. 연결 자체는 인증 및 권한 부여 이상의 특정 응용 프로그램 논리를 처리하지 않습니다.
연결이 identified_by :current_user
경우 그래서, 당신이 할 수있는 나중에 액세스 current_user
어디서든 FooChannel < ApplicationCable::Channel
내부! 예 :
class AppearanceChannel < ApplicationCable::Channel
def subscribed
stream_from "appearance_channel"
if current_user
ActionCable.server.broadcast "appearance_channel", { user: current_user.id, online: :on }
current_user.online = true
current_user.save!
end
end
def unsubscribed
if current_user
# Any cleanup needed when channel is unsubscribed
ActionCable.server.broadcast "appearance_channel", { user: current_user.id, online: :off }
current_user.online = false
current_user.save!
end
end
end
PS 난 당신이 레일에서 5 API를 쿠키를 사용하려면, 당신은 그것을에 전환 할 수 있습니다
http://guides.rubyonrails.org/api_app.html#other-middleware
설정/응용 프로그램을.RB
config.middleware.use ActionDispatch::Cookies
http://guides.rubyonrails.org/api_app.html#adding-other-modules
컨트롤러/API/application_controller.rb
class Api::ApplicationController < ActionController::API
include ActionController::Cookies
...
어떻게합니까? 당신은 토큰을 cable.js 파일에 넣을까요? 'App.cable = ActionCable.createConsumer ('/ cable/'+ token);'사용자로부터 토큰을 얻는 방법을 알 수 없다. –
cable.js가 없다. 이 파일은 Rails API 전용 케이스이므로 http://guides.rubyonrails.org/api_app.html에 있습니다. 문제는 여기에 설명되어 있습니다. https://stackoverflow.com/questions/13826594/coffeescript-access-to- 현재 사용자/43250865 # 43250865 – prograils