Module: Nyauth::SessionConcern

Extended by:
ActiveSupport::Concern
Included in:
ControllerConcern, 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



51
52
53
54
# File 'app/controllers/concerns/nyauth/session_concern.rb', line 51

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



40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/concerns/nyauth/session_concern.rb', line 40

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)

  unless signed_in?(options)
    session["#{options[:as]}_return_to"] = request.url if request.get?
    redirect_to new_session_path_for(options[:as])
  end
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_status(client)
end

#sign_outObject

ex.) sign_out



29
30
31
32
33
34
35
36
# File 'app/controllers/concerns/nyauth/session_concern.rb', line 29

def sign_out
  reset_session

  if Nyauth.configuration.use_cookie_auth
    # ex.) cookies.signed[:user_id]
    cookies.delete :nyauth_cookie_auth
  end
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_status(client) ⇒ Object



56
57
58
59
60
61
62
63
# File 'app/controllers/concerns/nyauth/session_concern.rb', line 56

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

  if Nyauth.configuration.use_cookie_auth
    # ex.) cookies.signed[:user_id]
    cookies.signed[:nyauth_cookie_auth] = "#{client.class.name.demodulize.underscore}:#{client.id}"
  end
end