1
votes

My app is authenticated and correctly displays information from an API call on its home page, but I can't get it to display information from an API call on a page using a proxy. The proxy is setup correctly, because it will display HTML if there is only HTML on its page (no Shopify API calls).

routes.rb

Rails.application.routes.draw do
  root :to => 'home#index'
  mount ShopifyApp::Engine, at: '/'
  get "/products" => "products#index", :as => :products
end

products_controller.rb

class ProductsController < ShopifyApp::AuthenticatedController
  around_filter :shopify_session      

  def index
    @productArray = ShopifyAPI::Product.find(:all, :params => {:limit => 10})
  end
end

index.html.erb

<h2>Products</h2>
<ul>
  <% @productArray.each do |product| %>
    <li><%= link_to product.title, "https://#{@shop_session.url}/admin/products/#{product.id}", target: "_top" %></li>
    <li><%= product.id %></li>
  <% end %>
</ul>

My Shopify Proxy

Path: a/products
Proxy url: https://appname.herokuapp.com/products

Now here's the weird part

When I go to https://devstorename.myshopify.com/a/products, it just redirects to https://devstorename.myshopify.com/password. No notifications or anything. If I removed the index function from the controller and changed the html file to just contain something like -

<h1>Test</h1>

Then it would correctly display "Test" on the screen with no redirection. My conclusion from this is that I probably have some sort of authentication issue where I don't have permission to make the calls on this page.

All of this code however works perfectly for the home_controller.rb controller and its index.html.erb, and the correct information gets displayed at the root route.

I'm obviously very new to this.

Edit

Using the app proxy gem,

products_controller.rb

class ProductsController < ShopifyApp::AppProxyController
  def index
    @productArray = ShopifyAPI::Product.find(:all, :params => {:limit => 10})
  end
end

Gemfile

source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.6'

group :development do
  gem 'sqlite3'
end

group :production do
  gem 'pg'
  gem 'rails_12factor'
end

gem 'shopify_app', :github => 'mikeyhew/shopify_app', :branch => 'app-proxy'

gem 'json'
# Use sqlite3 as the database for Active Record
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'


# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc


group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug'
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
end
1

1 Answers

0
votes

As far as I understand, ShopifyApp::AuthenticatedController isn't meant to be used behind an app proxy: you're only supposed to inherit from it in an embedded app.

As for being redirected to /password, it sounds like your storefront is password-protected. I recommend you turn that off before you continue working with app proxies, because it will confuse you.