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
Authenticate a record using cookies.
-
#build_passwordless_session(authenticatable) ⇒ Session
Build a new Passwordless::Session from an authenticatable record.
-
#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.
-
#sign_in(authenticatable) ⇒ ActiveRecord::Base
Signs in user by assigning their id to a permanent cookie.
-
#sign_out(authenticatable_class) ⇒ boolean
Signs out user by deleting their encrypted cookie.
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.
27 28 29 30 31 32 33 |
# File 'lib/passwordless/controller_helpers.rb', line 27 def (authenticatable_class) key = (authenticatable_class) authenticatable_id = .encrypted[key] return unless authenticatable_id authenticatable_class.find_by(id: authenticatable_id) 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`.
12 13 14 15 16 17 18 |
# File 'lib/passwordless/controller_helpers.rb', line 12 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 |
#reset_passwordless_redirect_location!(authenticatable_class) ⇒ String?
Resets the redirect_location to root_path by deleting the redirect_url from session.
68 69 70 |
# File 'lib/passwordless/controller_helpers.rb', line 68 def reset_passwordless_redirect_location!(authenticatable_class) session.delete session_key(authenticatable_class) end |
#save_passwordless_redirect_location!(authenticatable_class) ⇒ String
Saves request.original_url as the redirect location for a passwordless Model.
59 60 61 |
# File 'lib/passwordless/controller_helpers.rb', line 59 def save_passwordless_redirect_location!(authenticatable_class) session[session_key(authenticatable_class)] = request.original_url end |
#sign_in(authenticatable) ⇒ ActiveRecord::Base
Signs in user by assigning their id to a permanent cookie.
39 40 41 42 43 |
# File 'lib/passwordless/controller_helpers.rb', line 39 def sign_in(authenticatable) key = (authenticatable.class) .encrypted.permanent[key] = { value: authenticatable.id } authenticatable end |
#sign_out(authenticatable_class) ⇒ boolean
Signs out user by deleting their encrypted cookie.
48 49 50 51 52 53 |
# File 'lib/passwordless/controller_helpers.rb', line 48 def sign_out(authenticatable_class) key = (authenticatable_class) .encrypted.permanent[key] = { value: nil } .delete(key) true end |