Module: Sinatra::Shopify
- Defined in:
- lib/sinatra/shopify-sinatra-app.rb
Defined Under Namespace
Modules: Methods
Class Method Summary collapse
Class Method Details
.registered(app) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/sinatra/shopify-sinatra-app.rb', line 157 def self.registered(app) app.helpers Shopify::Methods app.register Sinatra::ActiveRecordExtension app.set :database_file, File.('config/database.yml') app.set :views, File.('views') app.set :public_folder, File.('public') app.set :erb, layout: :'layouts/application' app.set :protection, except: :frame_options app.enable :sessions app.enable :inline_templates app.set :scope, 'read_products, read_orders' app.set :api_key, ENV['SHOPIFY_API_KEY'] app.set :shared_secret, ENV['SHOPIFY_SHARED_SECRET'] app.set :secret, ENV['SECRET'] app.use Rack::Flash, sweep: true app.use Rack::MethodOverride app.use Rack::Session::Cookie, key: 'rack.session', path: '/', secret: app.settings.secret, expire_after: 60 * 30 # half an hour in seconds app.use OmniAuth::Builder do provider :shopify, app.settings.api_key, app.settings.shared_secret, scope: app.settings.scope, setup: lambda { |env| params = Rack::Utils.parse_query(env['QUERY_STRING']) site_url = "https://#{params['shop']}" env['omniauth.strategy'].[:client_options][:site] = site_url } end ShopifyAPI::Session.setup( api_key: app.settings.api_key, secret: app.settings.shared_secret ) app.get '/install' do if params[:shop].present? authenticate else erb :install, layout: false end end app.post '/login' do authenticate end app.get '/logout' do logout redirect '/install' end app.get '/auth/shopify/callback' do shop_name = params['shop'] token = request.env['omniauth.auth']['credentials']['token'] shop = Shop.find_or_initialize_by(name: shop_name) shop.token = token shop.save! session[:shopify] = { shop: shop_name, token: token } after_shopify_auth() return_to = env['omniauth.params']['return_to'] return_params = session[:return_params] session.delete(:return_params) return_to += "?#{return_params.to_query}" if return_params.present? redirect return_to end app.get '/auth/failure' do erb "<h1>Authentication Failed:</h1> <h3>message:<h3> <pre>#{params}</pre>", layout: false end end |