Sha256: a645d918e59f3e38c8207da4b22bb042de424ebe5a95e6d6433cbda6391fb3f3

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

module Insales::Controller
  module Autologin
    protected

    def insales_authenticate!
      params[:domain] ||= params[:shop]
      insales_logout if params[:domain].present? && params[:domain] != session[:domain]
      return if insales_authenticate_from_session

      store_location
      account = find_account_by_request
      return insales_autologin_start(account) if account

      redirect_to insales_login_path
    end

    def insales_authenticate_from_session
      data = session[:insales_session]
      return unless data && data[:account_id] && data[:insales_id]

      @account = account_class.where(account_id: data[:account_id])
                              .find_by_insales_id(data[:insales_id])
    end

    def insales_autologin_start(account)
      app = insales_app_class.new(account.insales_domain, account.insales_password)
      auth_url = app.authorization_url
      session[:insales_token] = app.auth_token
      session[:insales_token_data] = {
        domain: account.insales_domain,
        account_id: account.id,
        insales_id: account.insales_id,
      }
      redirect_to auth_url
    end

    def insales_autologin_finish(token = params[:token])
      if token && session[:insales_token] == token && session[:insales_token_data].is_a?(Hash)
        session[:insales_session] = session[:insales_token_data]
      end
      session[:insales_token] = session[:insales_token_data] = nil
      session[:account_id].present?
    end

    def insales_logout
      session.delete(:insales_session)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
insales_api-0.2.0 lib/insales/controller/autologin.rb