Sha256: 8db3a847a1381719cb8d14876b358fb115c3e47525c2d177b906aa6c34cf13d7

Contents?: true

Size: 892 Bytes

Versions: 7

Compression:

Stored size: 892 Bytes

Contents

require 'active_support/concern'

module CableConnectionConcern
  extend ActiveSupport::Concern
  included do
    identified_by :current_user

    def connect
      self.current_user = find_verified_user
    end

    protected

    def find_verified_user 
      # this checks whether a user is authenticated with devise
      # It looks for a token in the query parameters, or in the headers
      m = request.query_parameters["token"].presence || request.headers["Authorization"].split(" ").second.strip rescue nil

      body = ::HashWithIndifferentAccess.new(::JWT.decode(m, ::Rails.application.credentials.dig(:secret_key_base).presence||ENV["SECRET_KEY_BASE"], false)[0]) rescue nil
      if verified_user = (env['warden'].user.presence || User.find_by(id: body[:user_id]) rescue false)
        verified_user
      else
        reject_unauthorized_connection
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
thecore_backend_commons-3.2.3 config/initializers/concern_cable_connection.rb
thecore_backend_commons-3.2.2 config/initializers/concern_cable_connection.rb
thecore_backend_commons-3.2.1 config/initializers/concern_cable_connection.rb
thecore_backend_commons-3.2.0 config/initializers/concern_cable_connection.rb
thecore_backend_commons-3.1.9 config/initializers/concern_cable_connection.rb
thecore_backend_commons-3.1.8 config/initializers/concern_cable_connection.rb
thecore_backend_commons-3.1.7 config/initializers/concern_cable_connection.rb