Sha256: ee77b4064f100219f867db719f5ad69a0662b078a208c305768e77f949ff2a2d

Contents?: true

Size: 720 Bytes

Versions: 2

Compression:

Stored size: 720 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
      m = request.query_parameters["token"]

      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]))
        verified_user
      else
        reject_unauthorized_connection
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
thecore_backend_commons-3.1.5 config/initializers/concern_cable_connection.rb
thecore_backend_commons-3.1.4 config/initializers/concern_cable_connection.rb