Class: Clavis::AuthController

Inherits:
ActionController::Base
  • Object
show all
Includes:
Controllers::Concerns::Authentication, Controllers::Concerns::SessionManagement
Defined in:
app/controllers/clavis/auth_controller.rb

Overview

AuthController directly inherits from ActionController::Base to avoid inheriting host application's authentication requirements or before_actions that could interfere with the OAuth flow

Instance Method Summary collapse

Methods included from Controllers::Concerns::SessionManagement

#after_login_path, #after_logout_path, #authenticated?, #current_user, #sign_in_user, #sign_out_user, #store_location

Methods included from Controllers::Concerns::Authentication

#oauth_authorize, #oauth_callback

Instance Method Details

#authorizeObject



20
21
22
23
24
25
# File 'app/controllers/clavis/auth_controller.rb', line 20

def authorize
  # Store the current URL for returning after authentication
  store_location if request.get?

  oauth_authorize
end

#callbackObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/controllers/clavis/auth_controller.rb', line 27

def callback
  oauth_callback do |user, auth_hash|
    # This is a default implementation that can be overridden
    # by the application using the gem
    if respond_to?(:clavis_authentication_success)
      clavis_authentication_success(user, auth_hash)
    else
      # Use the SessionManagement method to sign in the user with secure cookies
      (user)

      # Get the redirect path
      redirect_path = 

      # Force redirect to root path if it's redirecting to auth path
      if redirect_path.include?("/auth/")
        redirect_path = defined?(main_app) && main_app.respond_to?(:root_path) ? main_app.root_path : "/"
      end

      redirect_to redirect_path, notice: "Successfully signed in with #{params[:provider].capitalize}"
    end
  end
rescue StandardError => e
  Clavis::Logging.log_error(e)

  if respond_to?(:clavis_authentication_failure)
    clavis_authentication_failure(e)
  else
    # Default behavior: redirect to sign in page with error
    flash[:alert] = case e
                    when Clavis::AuthorizationDenied
                      "Authentication was cancelled"
                    when Clavis::InvalidState, Clavis::MissingState
                      "Authentication session expired. Please try again."
                    else
                      "Authentication failed: #{e.message}"
                    end

    # Safe redirect to after_login_path
    redirect_to 
  end
end