Skip to content

Instantly share code, notes, and snippets.

@evsasse
Created April 14, 2021 23:49
Show Gist options
  • Save evsasse/184bdd3ee565d6d8e37d911728d7ce73 to your computer and use it in GitHub Desktop.
Save evsasse/184bdd3ee565d6d8e37d911728d7ce73 to your computer and use it in GitHub Desktop.
Fix for admin omniauth work side-by-side with Shopify shop authentication
--- a/app/models/admin.rb
+++ b/app/models/admin.rb
@@ -1,10 +1,12 @@
# frozen_string_literal: true
class Admin < ApplicationRecord
- devise :omniauthable, omniauth_providers: %i[developer google_oauth2]
-
+ # To not mess up Shopify's omniauth, we can't set omniauthable here!
+ # Check `config/routes/admin.rb` for more details about the custom sign in setup
+ devise :authenticatable
+
--- a/config/initializers/devise.rb
+++ b/config/initializers/devise.rb
@@ -272,13 +272,8 @@ Devise.setup do |config|
# Add a new OmniAuth provider. Check the wiki for more information on setting
# up on your models and hooks.
# config.omniauth :github, 'APP_ID', 'APP_SECRET', scope: 'user,public_repo'
- config.omniauth :developer
- config.omniauth(
- :google_oauth2,
- Rails.application.credentials.dig(:google, :oauth_client_id),
- Rails.application.credentials.dig(:google, :oauth_client_secret),
- hd: "superfiliate.com"
- )
+ # !!! DONT CHANGE OMNIAUTH CONFIGS HERE !!!
+ # !!! USE `omniauth.rb` INSTEAD !!!
# ==> Warden configuration
# If you want to use other strategies, that are not supported by Devise, or
--- a/config/initializers/omniauth.rb
+++ b/config/initializers/omniauth.rb
@@ -3,6 +3,16 @@
Rails.application.config.middleware.use(OmniAuth::Builder) do
# frozen_string_literal: true
+ # For logging in admins
+ # Check `config/routes/admin.rb` for more details about the custom sign in setup
+ provider :developer, path_prefix: '/admins/auth' if Rails.env.development?
+ provider :google_oauth2,
+ Rails.application.credentials.dig(:google, :oauth_client_id),
+ Rails.application.credentials.dig(:google, :oauth_client_secret),
+ path_prefix: '/admins/auth',
+ hd: "superfiliate.com"
+
+ # For logging in shops
provider :shopify,
ShopifyApp.configuration.api_key,
ShopifyApp.configuration.secret,
--- a/config/routes/admin.rb
+++ b/config/routes/admin.rb
@@ -1,8 +1,25 @@
# frozen_string_literal: true
get "/admin", to: redirect("/admins/sign_in")
+
+# https://github.com/Shopify/shopify_app/issues/149
-devise_for :admins, controllers: { omniauth_callbacks: "admins/omniauth_callbacks" }
+devise_for :admins
devise_scope :admin do
+ if Rails.env.development?
+ match "/admins/auth/developer",
+ via: %i[get post],
+ to: "admins/omniauth_callbacks#passthru",
+ as: :admin_developer_omniauth_authorize
+
+ match "/admins/auth/developer/callback",
+ via: %i[get post],
+ to: "admins/omniauth_callbacks#developer",
+ as: :admin_developer_omniauth_callback
+ end
+
+ match "/admins/auth/google_oauth2",
+ via: %i[get post],
+ to: "admins/omniauth_callbacks#passthru",
+ as: :admin_google_oauth2_omniauth_authorize
+ match "/admins/auth/google_oauth2/callback",
+ via: %i[get post],
+ to: "admins/omniauth_callbacks#google_oauth2",
+ as: :admin_google_oauth2_omniauth_callback
+
get "admins/sign_in", to: "admins/sessions#new", as: :new_admin_session
get "admins/sign_out", to: "admins/sessions#destroy", as: :destroy_admin_session
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment