Module: Sinatra::Shopify

Defined in:
lib/sinatra/shopify.rb

Defined Under Namespace

Modules: Helpers

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/sinatra/shopify.rb', line 23

def self.registered(app)
  app.helpers Shopify::Helpers
  app.enable :sessions

  app.template :login do
    <<-SRC
    <h1>Login</h1>

    <p>
      First you have to register this app with a shop to allow access to its private admin data.
    </p>

    <form action='/login/authenticate' method='post'>
      <label for='shop'><strong>The URL of the Shop</strong>
        <span class="hint">(or just the subdomain if it's at myshopify.com)</span>
      </label>
      <p>
        <input type='text' name='shop' />
      </p>
      <input type='submit' value='Authenticate' />
    </form>
      SRC
  end

  unless ENV['SHOPIFY_API_KEY'] && ENV['SHOPIFY_API_SECRET']
    puts "Set your Shopify api_key and secret via ENV['SHOPIFY_API_KEY'] and ENV['SHOPIFY_API_SECRET']"
  end
  ShopifyAPI::Session.setup(:api_key => ENV['SHOPIFY_API_KEY'], :secret => ENV['SHOPIFY_API_SECRET'])

  app.get '/login' do
    erb :login
  end
  
  app.get '/logout' do
    logout!
    redirect '/'
  end

  app.post '/login/authenticate' do
    redirect ShopifyAPI::Session.new(params[:shop]).create_permission_url
  end
  
  app.get '/login/finalize' do
    shopify_session = ShopifyAPI::Session.new(params[:shop], params[:t])
    if shopify_session.valid?
      session[:shopify] = shopify_session

      return_address = session[:return_to] || '/'
      session[:return_to] = nil
      redirect return_address
    else
      redirect '/login'
    end
  end
end