2016-06-07 3 views
0

새로운 Rails 5 (RC1) 앱을 만들고 있습니다. 사용자 인증을 위해 AuthLogic을 사용했으며, ActionCable에 들어갈 때까지 항상 작동합니다.AuthLogic이 ActionCable과 작동하도록하기

#app/channels/application_cable/connection.rb 
module ApplicationCable 
    class Connection < ActionCable::Connection::Base 
    identified_by :current_user 

    def connect 
     self.current_user = UserSession.find 
    end 
    end 
end 

나는 오류를 얻을 : 당신은 컨트롤러 객체에 Authlogic :: 세션 :: Base.controller를 활성화해야합니다 내가 노력

개체 만들기 전에 :

Authlogic::Session::Base.controller = Authlogic::ControllerAdapters::RailsAdapter.new(self) 

하지만 그건하지 않습니다 Connection 클래스가 Controller가 아니기 때문에 작동합니다.

AuthLogic 코드를 살펴 보았지만 컨트롤러 개체에 대한 의존성을 무시하는 방법을 알 수 없습니다. 사용자 세션을로드하기 만하면됩니다. 이견있는 사람?

답변

3

나는 그것을 스스로 알아 냈습니다. 나는 이것이 일종의 해킹이라고 생각합니다. 기본적으로 ApplicationController에서 AuthLogic persistence_token으로 보안 쿠키를 설정 한 다음이 토큰을 읽고 ActionCable에서 수동으로 사용자를로드 할 수 있습니다.

class ApplicationController < ActionController::Base 
    before_action :set_verify_cookie 

    def set_verify_cookie 
    #action cable needs a way outside of controller logic to lookup a user 
    return unless current_user 
    cookies.signed[:vvc] = current_user.persistence_token 
    end 
end 

#app/channels/connection.rb 
module ApplicationCable 
    class Connection < ActionCable::Connection::Base 
    identified_by :current_user 


    def connect 
     self.current_user = find_verified_user 
     logger.add_tags 'ActionCable', self.current_user.username unless self.current_user.nil? 
    end 

    protected 

    def find_verified_user_or_guest 
     User.find_by(:persistence_token => cookies.signed[:vvc]) 
    end 
end 

한 전위 gotch이 쿠키는 로그 아웃에 삭제해야하거나 ActionCable는 여전히 다음 페이지가로드의 사용자를 찾을 수 있습니다. 당신이 Authlogic 기본값을 사용하는 가정

#app/controllers/user_sessions_controller.rb 
class UserSessionsController < ApplicationController 

    def destroy 
    cookies.signed[:vvc] = nil 
    current_user_session.destroy 
    flash[:success] = "Logout successful!" 
    redirect_to root_url 
    end 
end 
+0

아주 좋은 솔루션을! 좋은 대답. 다음 라인을 편집하십시오 :'self.current_user = find_verified_user' :'self.current_user = find_verified_user_or_guest' – ZombieBsAs

0

는 지속성 토큰 키 'user_credentials'에서 쿠키에 저장됩니다.

그래서 당신은 다음과 같이 사용자를 조회 할 수 있습니다

# app/channels/application_cable/connection.rb 
module ApplicationCable 
    class Connection < ActionCable::Connection::Base 

    def connect 
     verify_user 
    end 

    private 
    def verify_user 
     reject_unauthorized_connection unless verified_user? 
    end 

    def verified_user? 
     cookie_key && User.find_by_persistence_token(token) 
    end 

    def token 
     cookie && cookie.include?('::') && cookie.split("::")[0] 
    end 

    def cookie 
    cookies['user_credentials'] 
    end 

    end 
end 
관련 문제