Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom OAuth URLs #1445

Merged
merged 2 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/shopify_app/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def authenticate
def start_oauth
auth_attributes = ShopifyAPI::Auth::Oauth.begin_auth(
shop: sanitized_shop_name,
redirect_path: "/auth/shopify/callback",
redirect_path: ShopifyApp.configuration.login_callback_url,
is_online: user_session_expected?
)
cookies.encrypted[auth_attributes[:cookie].name] = {
Expand Down
9 changes: 6 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# frozen_string_literal: true

ShopifyApp::Engine.routes.draw do
login_url = ShopifyApp.configuration.login_url.gsub(/^#{ShopifyApp.configuration.root_url}/, "")
login_callback_url = ShopifyApp.configuration.login_callback_url.gsub(/^#{ShopifyApp.configuration.root_url}/, "")

controller :sessions do
get "login" => :new, :as => :login
post "login" => :create, :as => :authenticate
get login_url => :new, :as => :login
post login_url => :create, :as => :authenticate
get "logout" => :destroy, :as => :logout
end

controller :callback do
get "auth/shopify/callback" => :callback
get login_callback_url => :callback
end

namespace :webhooks do
Expand Down
5 changes: 5 additions & 0 deletions lib/shopify_app/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Configuration
# customise urls
attr_accessor :root_url
attr_writer :login_url
attr_writer :login_callback_url

# customise ActiveJob queue names
attr_accessor :scripttags_manager_queue_name
Expand All @@ -50,6 +51,10 @@ def login_url
@login_url || File.join(@root_url, "login")
end

def login_callback_url
@login_callback_url || File.join(@root_url, "auth/shopify/callback")
end

def user_session_repository=(klass)
ShopifyApp::SessionRepository.user_storage = klass
end
Expand Down
26 changes: 26 additions & 0 deletions test/routes/callback_routes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,33 @@ class CallbackRoutesTest < ActionController::TestCase
ShopifyApp.configuration = nil
end

teardown do
ShopifyApp.configuration = nil
end

test "auth_shopify_callback routes to callback#callback" do
assert_routing "/auth/shopify/callback", controller: "shopify_app/callback", action: "callback"
end

test "callback route doesn't change with custom root URL because it is in an engine" do
ShopifyApp.configuration.root_url = "/new-root"
Rails.application.reload_routes!

assert_routing "/auth/shopify/callback", controller: "shopify_app/callback", action: "callback"
end

test "callback route changes with custom callback URL" do
ShopifyApp.configuration.login_callback_url = "/other-callback"
Rails.application.reload_routes!

assert_routing "/other-callback", controller: "shopify_app/callback", action: "callback"
end

test "callback route strips out root URL if both are set since it runs in an engine" do
ShopifyApp.configuration.root_url = "/new-root"
ShopifyApp.configuration.login_callback_url = "/new-root/other-callback"
Rails.application.reload_routes!

assert_routing "/other-callback", controller: "shopify_app/callback", action: "callback"
end
end
29 changes: 29 additions & 0 deletions test/routes/sessions_routes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class SessionsRoutesTest < ActionController::TestCase
ShopifyApp.configuration = nil
end

teardown do
ShopifyApp.configuration = nil
end

test "login routes to sessions#new" do
assert_routing "/login", { controller: "shopify_app/sessions", action: "new" }
end
Expand All @@ -20,4 +24,29 @@ class SessionsRoutesTest < ActionController::TestCase
test "logout routes to sessions#destroy" do
assert_routing "/logout", { controller: "shopify_app/sessions", action: "destroy" }
end

test "login route doesn't change with custom root URL because it is in an engine" do
ShopifyApp.configuration.root_url = "/new-root"
Rails.application.reload_routes!

assert_routing "/login", { controller: "shopify_app/sessions", action: "new" }
assert_routing({ method: "post", path: "/login" }, { controller: "shopify_app/sessions", action: "create" })
end

test "login route changes with custom login URL" do
ShopifyApp.configuration.login_url = "/other-login"
Rails.application.reload_routes!

assert_routing "/other-login", { controller: "shopify_app/sessions", action: "new" }
assert_routing({ method: "post", path: "/other-login" }, { controller: "shopify_app/sessions", action: "create" })
end

test "login route strips out root URL if both are set since it runs in an engine" do
ShopifyApp.configuration.root_url = "/new-root"
ShopifyApp.configuration.login_url = "/new-root/other-login"
Rails.application.reload_routes!

assert_routing "/other-login", { controller: "shopify_app/sessions", action: "new" }
assert_routing({ method: "post", path: "/other-login" }, { controller: "shopify_app/sessions", action: "create" })
end
end