Class: RailsSimpleAuth::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_simple_auth/configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



18
19
20
21
22
23
24
25
26
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
# File 'lib/rails_simple_auth/configuration.rb', line 18

def initialize
  @magic_link_enabled = true
  @email_confirmation_enabled = true
  @oauth_enabled = false
  @oauth_providers = []
  @oauth_provider_names = {}
  @oauth_link_existing_accounts = true # Allow OAuth to link to existing email accounts

  @magic_link_expiry = 15.minutes
  @password_reset_expiry = 15.minutes
  @confirmation_expiry = 24.hours
  @session_expiry = 30.days

  @rate_limits = {
    sign_in: { limit: 5, period: 15.minutes },
    sign_up: { limit: 5, period: 1.hour },
    magic_link: { limit: 3, period: 10.minutes },
    password_reset: { limit: 3, period: 1.hour },
    confirmation: { limit: 3, period: 1.hour }
  }

  @after_sign_in_path = :root_path
  @after_sign_out_path = :new_session_path
  @after_sign_up_path = :root_path
  @after_confirmation_path = :new_session_path

  @layout = 'rails_simple_auth'
  @app_name = nil

  @mailer_sender = 'noreply@example.com'
  @mailer_class = 'RailsSimpleAuth::AuthMailer'

  @user_class_name = 'User'
  @session_class_name = 'RailsSimpleAuth::Session'

  @password_minimum_length = 8

  @after_sign_in_callback = nil
  @after_sign_out_callback = nil
  @after_sign_up_callback = nil
  @after_confirmation_callback = nil

  @temporary_users_enabled = false
  @temporary_user_cleanup_days = 7
end

Instance Attribute Details

#after_confirmation_callbackObject

Returns the value of attribute after_confirmation_callback.



5
6
7
# File 'lib/rails_simple_auth/configuration.rb', line 5

def after_confirmation_callback
  @after_confirmation_callback
end

#after_confirmation_pathObject

Returns the value of attribute after_confirmation_path.



5
6
7
# File 'lib/rails_simple_auth/configuration.rb', line 5

def after_confirmation_path
  @after_confirmation_path
end

#after_sign_in_callbackObject

Returns the value of attribute after_sign_in_callback.



5
6
7
# File 'lib/rails_simple_auth/configuration.rb', line 5

def 
  @after_sign_in_callback
end

#after_sign_in_pathObject

Returns the value of attribute after_sign_in_path.



5
6
7
# File 'lib/rails_simple_auth/configuration.rb', line 5

def 
  @after_sign_in_path
end

#after_sign_out_callbackObject

Returns the value of attribute after_sign_out_callback.



5
6
7
# File 'lib/rails_simple_auth/configuration.rb', line 5

def after_sign_out_callback
  @after_sign_out_callback
end

#after_sign_out_pathObject

Returns the value of attribute after_sign_out_path.



5
6
7
# File 'lib/rails_simple_auth/configuration.rb', line 5

def after_sign_out_path
  @after_sign_out_path
end

#after_sign_up_callbackObject

Returns the value of attribute after_sign_up_callback.



5
6
7
# File 'lib/rails_simple_auth/configuration.rb', line 5

def 
  @after_sign_up_callback
end

#after_sign_up_pathObject

Returns the value of attribute after_sign_up_path.



5
6
7
# File 'lib/rails_simple_auth/configuration.rb', line 5

def 
  @after_sign_up_path
end

#app_nameObject

Returns the value of attribute app_name.



5
6
7
# File 'lib/rails_simple_auth/configuration.rb', line 5

def app_name
  @app_name
end

#confirmation_expiryObject

Returns the value of attribute confirmation_expiry.



5
6
7
# File 'lib/rails_simple_auth/configuration.rb', line 5

def confirmation_expiry
  @confirmation_expiry
end

#email_confirmation_enabledObject

Returns the value of attribute email_confirmation_enabled.



5
6
7
# File 'lib/rails_simple_auth/configuration.rb', line 5

def email_confirmation_enabled
  @email_confirmation_enabled
end

#layoutObject

Returns the value of attribute layout.



5
6
7
# File 'lib/rails_simple_auth/configuration.rb', line 5

def layout
  @layout
end

Returns the value of attribute magic_link_enabled.



5
6
7
# File 'lib/rails_simple_auth/configuration.rb', line 5

def magic_link_enabled
  @magic_link_enabled
end

Returns the value of attribute magic_link_expiry.



5
6
7
# File 'lib/rails_simple_auth/configuration.rb', line 5

def magic_link_expiry
  @magic_link_expiry
end

#mailer_classObject

Returns the value of attribute mailer_class.



5
6
7
# File 'lib/rails_simple_auth/configuration.rb', line 5

def mailer_class
  @mailer_class
end

#mailer_senderObject

Returns the value of attribute mailer_sender.



5
6
7
# File 'lib/rails_simple_auth/configuration.rb', line 5

def mailer_sender
  @mailer_sender
end

#oauth_enabledObject

Returns the value of attribute oauth_enabled.



5
6
7
# File 'lib/rails_simple_auth/configuration.rb', line 5

def oauth_enabled
  @oauth_enabled
end

Returns the value of attribute oauth_link_existing_accounts.



5
6
7
# File 'lib/rails_simple_auth/configuration.rb', line 5

def oauth_link_existing_accounts
  @oauth_link_existing_accounts
end

#oauth_provider_namesObject

Returns the value of attribute oauth_provider_names.



5
6
7
# File 'lib/rails_simple_auth/configuration.rb', line 5

def oauth_provider_names
  @oauth_provider_names
end

#oauth_providersObject

Returns the value of attribute oauth_providers.



5
6
7
# File 'lib/rails_simple_auth/configuration.rb', line 5

def oauth_providers
  @oauth_providers
end

#password_minimum_lengthObject

Returns the value of attribute password_minimum_length.



5
6
7
# File 'lib/rails_simple_auth/configuration.rb', line 5

def password_minimum_length
  @password_minimum_length
end

#password_reset_expiryObject

Returns the value of attribute password_reset_expiry.



5
6
7
# File 'lib/rails_simple_auth/configuration.rb', line 5

def password_reset_expiry
  @password_reset_expiry
end

#rate_limitsObject

Returns the value of attribute rate_limits.



5
6
7
# File 'lib/rails_simple_auth/configuration.rb', line 5

def rate_limits
  @rate_limits
end

#session_class_nameObject

Returns the value of attribute session_class_name.



5
6
7
# File 'lib/rails_simple_auth/configuration.rb', line 5

def session_class_name
  @session_class_name
end

#session_expiryObject

Returns the value of attribute session_expiry.



5
6
7
# File 'lib/rails_simple_auth/configuration.rb', line 5

def session_expiry
  @session_expiry
end

#temporary_user_cleanup_daysObject

Returns the value of attribute temporary_user_cleanup_days.



16
17
18
# File 'lib/rails_simple_auth/configuration.rb', line 16

def temporary_user_cleanup_days
  @temporary_user_cleanup_days
end

#temporary_users_enabledObject

Returns the value of attribute temporary_users_enabled.



5
6
7
# File 'lib/rails_simple_auth/configuration.rb', line 5

def temporary_users_enabled
  @temporary_users_enabled
end

#user_class_nameObject

Returns the value of attribute user_class_name.



5
6
7
# File 'lib/rails_simple_auth/configuration.rb', line 5

def user_class_name
  @user_class_name
end

Instance Method Details

#enable_oauth(*providers) ⇒ Object

Enable OAuth providers with optional display names Usage:

enable_oauth(:google_oauth2, :github)  # Uses default display names
enable_oauth(google_oauth2: "Google", github: "GitHub")  # Custom display names


95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rails_simple_auth/configuration.rb', line 95

def enable_oauth(*providers)
  self.oauth_enabled = true

  if providers.length == 1 && providers.first.is_a?(Hash)
    # Hash format: { google_oauth2: "Google", github: "GitHub" }
    provider_hash = providers.first
    self.oauth_providers = provider_hash.keys.map(&:to_sym)
    self.oauth_provider_names = provider_hash.transform_keys(&:to_sym)
  else
    # Symbol format: :google_oauth2, :github (backward compatible)
    self.oauth_providers = providers.map(&:to_sym)
    self.oauth_provider_names = {}
  end
end

#mailerObject



82
83
84
85
86
87
88
89
# File 'lib/rails_simple_auth/configuration.rb', line 82

def mailer
  mailer_class.constantize
rescue NameError => e
  raise ConfigurationError,
        "Mailer class '#{mailer_class}' not found. " \
        "Ensure it's defined and the name is correct in the initializer. " \
        "Original error: #{e.message}"
end

#oauth_provider_display_name(provider) ⇒ Object

Get display name for an OAuth provider Falls back to titleized provider name if no custom name is configured



116
117
118
119
# File 'lib/rails_simple_auth/configuration.rb', line 116

def oauth_provider_display_name(provider)
  provider_sym = provider.to_sym
  oauth_provider_names[provider_sym] || provider.to_s.gsub(/_oauth2$/, '').titleize
end

#oauth_provider_enabled?(provider) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/rails_simple_auth/configuration.rb', line 110

def oauth_provider_enabled?(provider)
  oauth_enabled && oauth_providers.include?(provider.to_sym)
end

#rate_limit_for(action) ⇒ Object



121
122
123
# File 'lib/rails_simple_auth/configuration.rb', line 121

def rate_limit_for(action)
  rate_limits&.dig(action.to_sym)
end

#session_classObject



73
74
75
76
77
78
79
80
# File 'lib/rails_simple_auth/configuration.rb', line 73

def session_class
  session_class_name.constantize
rescue NameError => e
  raise ConfigurationError,
        "Session class '#{session_class_name}' not found. " \
        "Ensure it's defined and the name is correct in the initializer. " \
        "Original error: #{e.message}"
end

#user_classObject



64
65
66
67
68
69
70
71
# File 'lib/rails_simple_auth/configuration.rb', line 64

def user_class
  user_class_name.constantize
rescue NameError => e
  raise ConfigurationError,
        "User class '#{user_class_name}' not found. " \
        "Ensure it's defined and the name is correct in the initializer. " \
        "Original error: #{e.message}"
end