Send auth_token for authentication to ActionCable

后端 未结 10 1548
梦毁少年i
梦毁少年i 2021-02-04 02:23
module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_user

    def connect
      #puts params[:auth_token]
      self         


        
10条回答
  •  温柔的废话
    2021-02-04 03:09

    In case any of you would like to use ActionCable.createCustomer. But have renewable token as I do:

    const consumer = ActionCable.createConsumer("/cable")
    const consumer_url = consumer.url
    Object.defineProperty(
      consumer, 
      'url', 
      {
          get: function() { 
            const token = localStorage.getItem('auth-token')
            const email = localStorage.getItem('auth-email')
            return consumer_url+"?email="+email+"&token="+token
          }
      });
    return consumer; 
    

    Then in case that the connection is lost it will be opened with a fresh new token.

提交回复
热议问题