Module: Passwordless::ControllerHelpers
- Included in:
- SessionsController
- Defined in:
- lib/passwordless/controller_helpers.rb
Overview
Helpers to work with Passwordless sessions from controllers
Instance Method Summary collapse
- #authenticate_by_cookie(authenticatable_class) ⇒ ActiveRecord::Base|nil deprecated Deprecated.
-
#authenticate_by_session(authenticatable_class) ⇒ ActiveRecord::Base|nil
Authenticate a record using the session.
-
#build_passwordless_session(authenticatable) ⇒ Session
Build a new Passwordless::Session from an authenticatable record.
-
#find_passwordless_session_for(authenticatable_class) ⇒ Session?
Returns the Session (if set) from the session.
- #redirect_session_key(authenticatable_class) ⇒ Object
-
#reset_passwordless_redirect_location!(authenticatable_class) ⇒ String?
Resets the redirect_location to root_path by deleting the redirect_url from session.
-
#save_passwordless_redirect_location!(authenticatable_class) ⇒ String
Saves request.original_url as the redirect location for a passwordless Model.
- #session_key(authenticatable_class) ⇒ Object
-
#sign_in(record) ⇒ ActiveRecord::Base
Signs in session to sign in.
-
#sign_out(authenticatable_class) ⇒ boolean
Signs out user by deleting the session key.
- #upgrade_passwordless_cookie(authenticatable_class) ⇒ Object
Instance Method Details
#authenticate_by_cookie(authenticatable_class) ⇒ ActiveRecord::Base|nil
Authenticate a record using cookies. Looks for a cookie corresponding to the authenticatable_class. If found try to find it in the database.
34 35 36 37 38 39 40 41 |
# File 'lib/passwordless/controller_helpers.rb', line 34 def (authenticatable_class) key = (authenticatable_class) authenticatable_id = .encrypted[key] return authenticatable_class.find_by(id: authenticatable_id) if authenticatable_id authenticate_by_session(authenticatable_class) end |
#authenticate_by_session(authenticatable_class) ⇒ ActiveRecord::Base|nil
Authenticate a record using the session. Looks for a session key corresponding to the authenticatable_class. If found try to find it in the database.
66 67 68 69 |
# File 'lib/passwordless/controller_helpers.rb', line 66 def authenticate_by_session(authenticatable_class) return unless find_passwordless_session_for(authenticatable_class)&.available? find_passwordless_session_for(authenticatable_class).authenticatable end |
#build_passwordless_session(authenticatable) ⇒ Session
Build a new Passwordless::Session from an authenticatable record. Set’s ‘user_agent` and `remote_addr` from Rails’ ‘request`.
18 19 20 21 22 23 24 |
# File 'lib/passwordless/controller_helpers.rb', line 18 def build_passwordless_session(authenticatable) Session.new.tap do |us| us.remote_addr = request.remote_addr us.user_agent = request.env["HTTP_USER_AGENT"] us.authenticatable = authenticatable end end |
#find_passwordless_session_for(authenticatable_class) ⇒ Session?
Returns the Session (if set) from the session.
8 9 10 |
# File 'lib/passwordless/controller_helpers.rb', line 8 def find_passwordless_session_for(authenticatable_class) Passwordless::Session.find_by(id: session[session_key(authenticatable_class)]) end |
#redirect_session_key(authenticatable_class) ⇒ Object
135 136 137 |
# File 'lib/passwordless/controller_helpers.rb', line 135 def redirect_session_key(authenticatable_class) :"passwordless_prev_location--#{authenticatable_class_parameterized(authenticatable_class)}" end |
#reset_passwordless_redirect_location!(authenticatable_class) ⇒ String?
Resets the redirect_location to root_path by deleting the redirect_url from session.
127 128 129 |
# File 'lib/passwordless/controller_helpers.rb', line 127 def reset_passwordless_redirect_location!(authenticatable_class) session.delete(redirect_session_key(authenticatable_class)) end |
#save_passwordless_redirect_location!(authenticatable_class) ⇒ String
Saves request.original_url as the redirect location for a passwordless Model.
118 119 120 |
# File 'lib/passwordless/controller_helpers.rb', line 118 def save_passwordless_redirect_location!(authenticatable_class) session[redirect_session_key(authenticatable_class)] = request.original_url end |
#session_key(authenticatable_class) ⇒ Object
131 132 133 |
# File 'lib/passwordless/controller_helpers.rb', line 131 def session_key(authenticatable_class) :"passwordless_session_id--#{authenticatable_class_parameterized(authenticatable_class)}" end |
#sign_in(record) ⇒ ActiveRecord::Base
Signs in session to sign in
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/passwordless/controller_helpers.rb', line 75 def sign_in(record) passwordless_session = if record.is_a?(Passwordless::Session) record else warn "Passwordless::ControllerHelpers#sign_in with authenticatable " \ "(`#{record.class}') is deprecated. Falling back to creating a " \ "new Passwordless::Session" build_passwordless_session(record).tap { |s| s.save! } end passwordless_session.claim! if Passwordless.restrict_token_reuse raise Passwordless::Errors::SessionTimedOutError if passwordless_session.timed_out? key = session_key(passwordless_session.authenticatable_type) session[key] = passwordless_session.id if record.is_a?(Passwordless::Session) passwordless_session else passwordless_session.authenticatable end end |
#sign_out(authenticatable_class) ⇒ boolean
Signs out user by deleting the session key.
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/passwordless/controller_helpers.rb', line 103 def sign_out(authenticatable_class) # Deprecated - cookies key = (authenticatable_class) .encrypted.permanent[key] = {value: nil} .delete(key) # /deprecated reset_session true end |
#upgrade_passwordless_cookie(authenticatable_class) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/passwordless/controller_helpers.rb', line 44 def (authenticatable_class) key = (authenticatable_class) return unless (authenticatable_id = .encrypted[key]) .encrypted.permanent[key] = {value: nil} .delete(key) return unless (record = authenticatable_class.find_by(id: authenticatable_id)) new_session = build_passwordless_session(record).tap { |s| s.save! } sign_in new_session new_session.authenticatable end |