2016-07-20 8 views
5

मैं डेविस के साथ एक्शन केबल काम करने की कोशिश कर रहा हूं।रेल डिवाइसेज एक्शन केबल

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 

तो में मैं अभी भी से cookies.signed['user.id']

उत्तर

4

nil प्राप्त एक वार्डन कॉलबैक में कुकी सेट करने का प्रयास एक उपयोगकर्ता लॉग होता है।

`config करने के लिए फ़ाइल जोड़ें/initializers/your_file.rb``

फाइल करने के लिए इस जोड़ें:

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 

या आप कुछ इस तरह कर सकता है:

verified_user = env['warden'].user 

के रूप में यह बहुत ही अच्छा tuorial में विस्तार से बताया: https://www.sitepoint.com/create-a-chat-app-with-rails-5-actioncable-and-devise/

+2

यह सिवाय इसके कि मेरे उपयोगकर्ता डिफ़ॉल्ट आप वसीयत के साथ स्थापित नहीं था मेरे लिए अच्छी तरह से काम किया। । 'वार्डन :: प्रबंधक: उदाहरणों आप किसी भिन्न उपयोगकर्ता सिर्फ इसलिए' verfied_user = [ 'वार्डन'] env उपयोगकर्ता ('admin_user') ' – Timbinous

+1

सही लॉगआउट कोड की तरह है कि उपयोगकर्ता प्रकार जोड़ में अगले तर्क के रूप में लॉग इन किया है के लिए .before_logout do | उपयोगकर्ता, auth, opts | गुंजाइश = विकल्प चुनता [: गुंजाइश] auth.cookies.delete ("# {scope} .id") auth.cookies.delete ("# {scope} .expires_at") end' – prograils

6

अद्यतन अपनेनिम्नलिखित के साथ:

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 

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

+0

क्या रेल के संस्करण/वसीयत/वार्डन यह काम करने के लिए लगता है? 'Env [ 'वार्डन']। User' –

+0

@ArnoldRoa यह एक मूल्य केवल जब आप पहले से ही प्रमाणीकरण कहीं और से पहले, यानि कि पहले से ही एक क्लासिक वसीयत प्रपत्र का उपयोग करके प्रवेश कर लिए हैं, शामिल नहीं के बराबर होता है। यह एक कुकी सेट करेगा और इसके बाद, वार्डन के रैक मिडलवेयर प्रत्येक अनुरोध पर 'env ['warden'] उपयोगकर्ता को सेट करेगा। –

संबंधित मुद्दे