Created
April 13, 2018 01:07
-
-
Save jordanhudgens/2e01b9d47aa88040f7e232a49d55dcf5 to your computer and use it in GitHub Desktop.
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
module ShopifyApp | |
class SessionsController < ActionController::Base | |
include ShopifyApp::LoginProtection | |
layout false, only: :new | |
after_action only: :new do |controller| | |
controller.response.headers.except!('X-Frame-Options') | |
end | |
def new | |
authenticate if sanitized_shop_name.present? | |
end | |
def create | |
authenticate | |
end | |
def callback | |
if auth_hash | |
login_shop | |
install_webhooks | |
install_scripttags | |
create_recurring_application_charge | |
perform_after_authenticate_job | |
else | |
flash[:error] = I18n.t('could_not_log_in') | |
redirect_to login_url | |
end | |
end | |
def destroy | |
reset_session | |
flash[:notice] = I18n.t('.logged_out') | |
redirect_to login_url | |
end | |
private | |
def create_recurring_application_charge | |
sess = ShopifyAPI::Session.new(shop_name, token) | |
ShopifyAPI::Base.activate_session(sess) | |
unless ShopifyAPI::RecurringApplicationCharge.current | |
recurring_application_charge = ShopifyAPI::RecurringApplicationCharge.new | |
recurring_application_charge.name = "It's free to use the Shop Hacker marketplace" | |
recurring_application_charge.test = false | |
recurring_application_charge.price = 0.00 | |
recurring_application_charge.terms = "You will be automatically charged the merchant cost for each Shop Hacker product your customers order." | |
recurring_application_charge.capped_amount = 150 | |
recurring_application_charge.return_url = "https://shop-hacker-shopify-app.herokuapp.com/activatecharge" | |
if recurring_application_charge.save | |
redirect_to recurring_application_charge.confirmation_url | |
end | |
end | |
end | |
def authenticate | |
if sanitized_shop_name.present? | |
session['shopify.omniauth_params'] = { shop: sanitized_shop_name } | |
fullpage_redirect_to "#{main_app.root_path}auth/shopify" | |
else | |
flash[:error] = I18n.t('invalid_shop_url') | |
redirect_to return_address | |
end | |
end | |
def login_shop | |
sess = ShopifyAPI::Session.new(shop_name, token) | |
request.session_options[:renew] = true | |
session.delete(:_csrf_token) | |
session[:shopify] = ShopifyApp::SessionRepository.store(sess) | |
session[:shopify_domain] = shop_name | |
session[:shopify_user] = associated_user if associated_user.present? | |
end | |
def auth_hash | |
request.env['omniauth.auth'] | |
end | |
def shop_name | |
auth_hash.uid | |
end | |
def associated_user | |
return unless auth_hash['extra'].present? | |
auth_hash['extra']['associated_user'] | |
end | |
def token | |
auth_hash['credentials']['token'] | |
end | |
def install_webhooks | |
return unless ShopifyApp.configuration.has_webhooks? | |
WebhooksManager.queue( | |
shop_name, | |
token, | |
ShopifyApp.configuration.webhooks | |
) | |
end | |
def install_scripttags | |
return unless ShopifyApp.configuration.has_scripttags? | |
ScripttagsManager.queue( | |
shop_name, | |
token, | |
ShopifyApp.configuration.scripttags | |
) | |
end | |
def perform_after_authenticate_job | |
config = ShopifyApp.configuration.after_authenticate_job | |
return unless config && config[:job].present? | |
if config[:inline] == true | |
config[:job].perform_now(shop_domain: session[:shopify_domain]) | |
else | |
config[:job].perform_later(shop_domain: session[:shopify_domain]) | |
end | |
end | |
def return_address | |
session.delete(:return_to) || ShopifyApp::configuration.root_url | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment