Skip to content

Commit 2b72e84

Browse files
committed
Redirect to /admin (not host root) after successful SSO sign-in
OmniauthCallbacksController previously relied on Devise's default `after_sign_in_path_for`, which falls back to the host app's `/` route. Hosts without a `root` defined hit a routing error; hosts that do define one (e.g. Rails' welcome page) land somewhere they don't want to be. Override `after_sign_in_path_for` to return `/admin` directly, so the gem keeps working for any ActiveAdmin host without assuming a root route. Honours `stored_location_for` for deep-link returns. Also drop the `root to: redirect("/admin")` line from the dummy app routes — the request spec would previously pass either way because `/` forwarded to `/admin`. The tighter assertion (response.location path == "/admin") would have been a false green there.
1 parent b51a33f commit 2b72e84

3 files changed

Lines changed: 28 additions & 3 deletions

File tree

app/controllers/active_admin/oidc/devise/omniauth_callbacks_controller.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ def failure
4545
flash[:alert] = ActiveAdmin::Oidc.config.access_denied_message
4646
redirect_to after_omniauth_failure_path_for(resource_name)
4747
end
48+
49+
private
50+
51+
# Land on the ActiveAdmin namespace root after a successful SSO
52+
# sign-in instead of Devise's default (host app root). Hosts
53+
# that don't define a `/` route would otherwise hit a routing
54+
# error immediately after login, and even when `/` does exist
55+
# it's rarely what an admin user wants to see. ActiveAdmin
56+
# always mounts at `/admin`, so we go there directly.
57+
def after_sign_in_path_for(resource)
58+
stored_location_for(resource) || "/admin"
59+
end
4860
end
4961
end
5062
end

spec/dummy/config/routes.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,9 @@
33
Rails.application.routes.draw do
44
devise_for :admin_users, ActiveAdmin::Devise.config
55
ActiveAdmin.routes(self)
6-
root to: redirect("/admin")
6+
# Intentionally no `root` route. The gem's OmniauthCallbacksController
7+
# must redirect into the ActiveAdmin namespace directly — not rely on
8+
# the host app to supply a `/` route. If the redirect regresses to
9+
# Devise's default, these request specs will break with a missing
10+
# route error, which is exactly what we want.
711
end

spec/requests/omniauth_callback_spec.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,18 @@ def trigger_callback
5757
expect(user.email).to eq("new@example.com")
5858
expect(user.oidc_raw_info).to include("sub" => "sub-new", "email" => "new@example.com")
5959

60-
# Devise's default is the app root; the dummy app redirects `/` to /admin.
6160
expect(response).to be_redirect
62-
expect(response.location).to match(%r{\Ahttps?://[^/]+/(\z|admin)})
61+
end
62+
63+
it "redirects directly to the ActiveAdmin namespace root, not the host app's /" do
64+
OmniAuth.config.mock_auth[:oidc] =
65+
build_auth_hash(uid: "sub-new", email: "new@example.com")
66+
67+
post "/admin/auth/oidc"
68+
follow_redirect! # OmniAuth request phase -> callback
69+
# `response` is now whatever the callbacks controller redirected to.
70+
expect(response).to be_redirect
71+
expect(URI(response.location).path).to eq("/admin")
6372
end
6473
end
6574

0 commit comments

Comments
 (0)