Module: Nyauth::SessionConcern

Extended by:
ActiveSupport::Concern
Included in:
PasswordsController, SessionsController, Test::ControllerMacros, Test::FeatureMacros
Defined in:
app/controllers/concerns/nyauth/session_concern.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#current_authenticated(options = {}) ⇒ Object



42
43
44
45
# File 'app/controllers/concerns/nyauth/session_concern.rb', line 42

def current_authenticated(options = {})
  options.reverse_merge!(as: :user)
  nyauth_nyan.session.fetch(options[:as])
end

#require_authentication!(options = {}) ⇒ Object

ex.) before_action -> { require_authentication! as: :user }, only: :secret_action



35
36
37
38
39
40
# File 'app/controllers/concerns/nyauth/session_concern.rb', line 35

def require_authentication!(options = {})
  options.reverse_merge!(as: :user)
  return if self.class.allow_actions == :all
  return if self.class.allow_actions.present? && request[:action].to_sym.in?(self.class.allow_actions)
  head :unauthorized unless signed_in?(options)
end

#sign_in(client) ⇒ Object

ex.) sign_in(client)



14
15
16
17
# File 'app/controllers/concerns/nyauth/session_concern.rb', line 14

def (client)
  return unless client
  store_signed_in_session(client)
end

#sign_outObject

ex.) sign_out



29
30
31
# File 'app/controllers/concerns/nyauth/session_concern.rb', line 29

def sign_out
  reset_session
end

#signed_in?(options = {}) ⇒ Boolean

ex.) signed_in?(as: :user)

Returns:

  • (Boolean)


21
22
23
24
25
# File 'app/controllers/concerns/nyauth/session_concern.rb', line 21

def signed_in?(options = {})
  options.reverse_merge!(as: :user)
  current_authenticated(options).present? && \
    current_authenticated(options).class.name.demodulize.underscore == options[:as].to_s
end

#store_signed_in_session(client) ⇒ Object



47
48
49
# File 'app/controllers/concerns/nyauth/session_concern.rb', line 47

def store_signed_in_session(client)
  nyauth_nyan.session.store(client, client.class.name.demodulize.underscore)
end