Created
May 22, 2014 15:22
-
-
Save agmcleod/7106363317ebd082d3df to your computer and use it in GitHub Desktop.
My shopify auth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::Base | |
protect_from_forgery | |
force_ssl | |
helper_method :current_shop, :shopify_session | |
protected | |
def current_shop | |
@current_shop ||= Shop.find(session[:shop_id]) if session[:shop_id].present? | |
end | |
def shopify_session | |
if current_shop.nil? | |
redirect_to new_login_url | |
else | |
begin | |
session = ShopifyAPI::Session.new(current_shop.url, current_shop.token) | |
ShopifyAPI::Base.activate_session(session) | |
yield | |
ensure | |
ShopifyAPI::Base.clear_session | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create | |
omniauth = request.env['omniauth.auth'] | |
if omniauth && omniauth[:provider] && omniauth[:provider] == "shopify" | |
shop = Shop.find_or_create_by_url(params[:shop].gsub(/https?\:\/\//, "")) | |
shop.update_attribute(:token, omniauth['credentials'].token) | |
shopify_session = ShopifyAPI::Session.new(shop.url, shop.token) | |
session[:shop_id] = shop.to_param | |
redirect_to root_url | |
else | |
flash[:error] = "Something went wrong" | |
redirect_to root_url | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# goes in config/initializers/ | |
Rails.application.config.middleware.use OmniAuth::Builder do | |
provider :shopify, Settings.api_key, Settings.api_secret, | |
scope: 'write_products,write_script_tags,read_orders', | |
setup: lambda { |env| params = Rack::Utils.parse_query(env['QUERY_STRING']) | |
env['omniauth.strategy'].options[:client_options][:site] = "http://#{params['shop']}" } | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# then this to support the above controller in my routes | |
match '/auth/shopify/callback', :to => 'login#create' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment