Sha256: b161f991cf0b5d50062afa66312f56978135ba209714270b1d6f18296e276887

Contents?: true

Size: 1.28 KB

Versions: 13

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

module ShopifyApp
  module UserSessionStorage
    extend ActiveSupport::Concern
    include ::ShopifyApp::SessionStorage

    included do
      validates :shopify_domain, presence: true
    end

    class_methods do
      def store(auth_session, user)
        user = find_or_initialize_by(shopify_user_id: user.id)
        user.shopify_token = auth_session.access_token
        user.shopify_domain = auth_session.shop
        user.save!
        user.id
      end

      def retrieve(id)
        user = find_by(id: id)
        construct_session(user)
      end

      def retrieve_by_shopify_user_id(user_id)
        user = find_by(shopify_user_id: user_id)
        construct_session(user)
      end

      private

      def construct_session(user)
        return unless user

        associated_user = ShopifyAPI::Auth::AssociatedUser.new(
          id: user.shopify_user_id,
          first_name: "",
          last_name: "",
          email: "",
          email_verified: false,
          account_owner: false,
          locale: "",
          collaborator: false,
        )

        ShopifyAPI::Auth::Session.new(
          shop: user.shopify_domain,
          access_token: user.shopify_token,
          associated_user: associated_user,
        )
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
shopify_app-21.9.0 lib/shopify_app/session/user_session_storage.rb
shopify_app-21.8.1 lib/shopify_app/session/user_session_storage.rb
shopify_app-21.8.0 lib/shopify_app/session/user_session_storage.rb
shopify_app-21.7.0 lib/shopify_app/session/user_session_storage.rb
shopify_app-21.6.0 lib/shopify_app/session/user_session_storage.rb
shopify_app-21.5.0 lib/shopify_app/session/user_session_storage.rb
shopify_app-21.4.1 lib/shopify_app/session/user_session_storage.rb
shopify_app-21.4.0 lib/shopify_app/session/user_session_storage.rb
shopify_app-21.3.1 lib/shopify_app/session/user_session_storage.rb
shopify_app-21.3.0 lib/shopify_app/session/user_session_storage.rb
shopify_app-21.2.0 lib/shopify_app/session/user_session_storage.rb
shopify_app-21.1.1 lib/shopify_app/session/user_session_storage.rb
shopify_app-21.1.0 lib/shopify_app/session/user_session_storage.rb