2016-07-20 17 views
5

Tôi đang cố gắng để có được cáp hành động làm việc với Devise.Rails Devise Cáp hành động

module ApplicationCable 
    class Connection < ActionCable::Connection::Base 

    identified_by :current_user 

    def connect 
     self.current_user = find_verified_user 
     logger.add_tags 'ActionCable', current_user.name 
    end 

    protected 

    def find_verified_user 
     verified_user = User.find_by(id: cookies.signed['user.id']) 
     if verified_user && cookies.signed['user.expires_at'] > Time.now 
     verified_user 
     else 
     reject_unauthorized_connection 
     end 
    end 
    end 
end 

Nếu một người dùng được đăng nhập vào tôi vẫn nhận được nil từ cookies.signed['user.id']

Trả lời

4

Hãy thử thiết lập các cookie trong một callback cai ngục.

Thêm một tập tin vào thư mục `config/initializers/your_file.rb``

Thêm phần này vào file:

Warden::Manager.after_set_user do |user, auth, opts| 
    scope = opts[:scope] 
    auth.cookies.signed["#{scope}.id"] = user.id 
    auth.cookies.signed["#{scope}.expires_at"] = 60.minutes.from_now 
end 

Warden::Manager.before_logout do |user, auth, opts| 
    scope = opts[:scope] 
    auth.cookies.signed["#{scope}.id"] = nil 
    auth.cookies.signed["#{scope}.expires_at"] = nil 
end 

Hoặc bạn có thể làm điều gì đó như thế này:

verified_user = env['warden'].user 

Như được giải thích trong giai đoạn này rất đẹp: https://www.sitepoint.com/create-a-chat-app-with-rails-5-actioncable-and-devise/

+2

này đã làm việc tốt cho tôi ngoại trừ việc sử dụng của tôi không phải là mặc định bạn thiết lập với devise. Đối với trường hợp bạn có một người dùng khác đăng nhập chỉ cần thêm kiểu người dùng đó làm đối số tiếp theo như vậy 'verfied_user = env ['warden']. User ('admin_user')' – Timbinous

+1

Mã đăng xuất phải: 'Warden :: Manager .before_logout do | user, auth, opts | scope = opts [: scope] auth.cookies.delete ("# {scope} .id") auth.cookies.delete ("# {scope} .expires_at") end' – prograils

6

Cập nhậtcủa bạnnhư sau:

module ApplicationCable 
    class Connection < ActionCable::Connection::Base 
    identified_by :current_user 

    def connect 
     self.current_user = find_verified_user 
     logger.add_tags 'ActionCable', current_user.studentid 
    end 

    protected 

    def find_verified_user # this checks whether a user is authenticated with devise 
     if verified_user = env['warden'].user 
     verified_user 
     else 
     reject_unauthorized_connection 
     end 
    end 
    end 
end 

Link: http://tutorials.pluralsight.com/ruby-ruby-on-rails/implementing-a-custom-devise-sign-in-and-actioncable-rails-5?saved=1&status=in-review

+0

Phiên bản rails/devise/warden này là giả sử để làm việc? 'env ['warden']. user' luôn là nil –

+0

@ArnoldRoa nó chỉ chứa giá trị khi bạn đã thực hiện xác thực ở một nơi khác trước đây, tức là đã đăng nhập bằng cách sử dụng một biểu mẫu phát triển cổ điển. Điều này sẽ thiết lập một cookie và sau đó, middleware rack của warden sẽ đặt 'env ['warden']. User' trên mỗi yêu cầu. –

Các vấn đề liên quan