Skip to content

Commit ce9eeec

Browse files
committed
Fix OAuth session persistence behind reverse proxy
- Revert to POST method for OAuth login - Add trusted proxies configuration for HTTPS detection - Change session cookie same_site from :lax to :none for OAuth - Add debug logging to OAuth callback for troubleshooting
1 parent 102e1da commit ce9eeec

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

app/controllers/sessions_controller.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@ def callback
1313

1414
# Store user ID in session
1515
session[:user_id] = user.id
16-
Rails.logger.info "Set session[:user_id] = #{user.id} for user #{user.username}"
16+
17+
# Debug logging
18+
Rails.logger.info "=== OAuth Callback Debug ==="
19+
Rails.logger.info "Session ID: #{session.id}"
20+
Rails.logger.info "User ID set: #{session[:user_id]}"
21+
Rails.logger.info "Request protocol: #{request.protocol}"
22+
Rails.logger.info "Request SSL?: #{request.ssl?}"
23+
Rails.logger.info "X-Forwarded-Proto: #{request.headers['X-Forwarded-Proto']}"
24+
Rails.logger.info "==========================="
1725

1826
# Redirect to birthday form if not provided, otherwise to dashboard
1927
if user.birthday_provided?

app/views/layouts/application.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
<%= button_to "Logout", logout_path, method: :delete,
5353
class: "inline-flex items-center px-4 py-2 border border-gray-300 text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 transition-colors" %>
5454
<% else %>
55-
<%= link_to "Login with GitHub", "/auth/github",
55+
<%= button_to "Login with GitHub", "/auth/github", method: :post,
5656
data: { turbo: false },
5757
class: "inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md text-white bg-gray-900 hover:bg-gray-800 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-gray-500 transition-colors" %>
5858
<% end %>

config/environments/production.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929

3030
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
3131
config.force_ssl = true
32+
33+
# If behind a proxy, add trusted proxies to properly detect HTTPS
34+
config.action_dispatch.trusted_proxies = ActionDispatch::RemoteIp::TRUSTED_PROXIES + ['10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16']
3235

3336
# Skip http-to-https redirect for the default health check endpoint.
3437
# config.ssl_options = { redirect: { exclude: ->(request) { request.path == "/up" } } }
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Configure session store for production
22
Rails.application.config.session_store :cookie_store,
33
key: '_pyramid_scheme_session',
4-
same_site: :lax,
4+
same_site: :none, # Changed from :lax for OAuth compatibility
55
secure: Rails.env.production?, # Only use secure cookies in production if HTTPS
66
httponly: true

0 commit comments

Comments
 (0)