Skip to content

Commit 63d144d

Browse files
committed
Fix production login issues
- Add exception handling for current_user when user_id doesn't exist in DB - Skip authentication on SessionsController, ReferralsController, and HomeController - Add debug logging to track authentication flow - Ensure public routes are properly excluded from auth requirements
1 parent 640bb15 commit 63d144d

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

app/controllers/application_controller.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ class ApplicationController < ActionController::Base
77
# Session management helpers
88
def current_user
99
@current_user ||= User.find(session[:user_id]) if session[:user_id]
10+
rescue ActiveRecord::RecordNotFound
11+
# Handle case where user_id in session doesn't exist in database
12+
session[:user_id] = nil
13+
nil
1014
end
1115

1216
def logged_in?
@@ -35,10 +39,14 @@ def require_admin
3539

3640
def require_login_for_authenticated_routes
3741
# Skip authentication for certain routes
38-
return if public_routes.include?(request.path) ||
39-
request.path.start_with?('/auth/') ||
40-
request.path.match?(/^\/[a-z0-9]+$/) # referral codes
42+
if public_routes.include?(request.path) ||
43+
request.path.start_with?('/auth/') ||
44+
request.path.match?(/^\/[a-z0-9]+$/) # referral codes
45+
Rails.logger.info "Skipping auth for #{request.path}"
46+
return
47+
end
4148

49+
Rails.logger.info "Requiring auth for #{request.path}, logged_in?: #{logged_in?}"
4250
require_login
4351
end
4452

app/controllers/home_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class HomeController < ApplicationController
22
include ActionView::Helpers::DateHelper
3+
skip_before_action :require_login_for_authenticated_routes
34
def index
45
@recent_visits = Visit.includes(:user)
56
.recent

app/controllers/referrals_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class ReferralsController < ApplicationController
22
include ApplicationHelper
3+
skip_before_action :require_login_for_authenticated_routes
34
def show
45
@referral_code = params[:referral_code]
56
@referrer = User.find_by(referral_code: @referral_code)

app/controllers/sessions_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class SessionsController < ApplicationController
2+
skip_before_action :require_login_for_authenticated_routes
23
def github
34
# This action is no longer needed - we'll use a form that POSTs directly to /auth/github
45
redirect_to '/auth/github', allow_other_host: true

0 commit comments

Comments
 (0)