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

Instance Method Details

Authenticate a record using cookies. Looks for a cookie corresponding to the authenticatable_class. If found try to find it in the database.

Parameters:

  • authenticatable_class (ActiveRecord::Base)

    any Model connected to passwordless. (e.g - User or Admin).

Returns:

  • (ActiveRecord::Base|nil)

    an instance of Model found by id stored in cookies.encrypted or nil if nothing is found.

See Also:



27
28
29
30
31
32
33
# File 'lib/passwordless/controller_helpers.rb', line 27

def authenticate_by_cookie(authenticatable_class)
  key = cookie_name(authenticatable_class)
  authenticatable_id = cookies.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`.

Parameters:

  • authenticatable (ActiveRecord::Base)

    Instance of an authenticatable Rails model

Returns:

  • (Session)

    the new Session object

See Also:



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.

Parameters:

  • authenticatable_class (ActiveRecord::Base)

    any Model connected to passwordless. (e.g - User or Admin).

Returns:

  • (String, nil)

    the redirect url that was just deleted, or nil if no url found for given Model.



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.

Parameters:

  • authenticatable_class (ActiveRecord::Base)

    any Model connected to passwordless. (e.g - User or Admin).

Returns:

  • (String)

    the redirect url that was just saved.



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.

Parameters:

  • authenticatable (ActiveRecord::Base)

    Instance of Model to sign in (e.g - @user when @user = User.find(id: some_id)).

Returns:

  • (ActiveRecord::Base)

    the record that is passed in.



39
40
41
42
43
# File 'lib/passwordless/controller_helpers.rb', line 39

def (authenticatable)
  key = cookie_name(authenticatable.class)
  cookies.encrypted.permanent[key] = { value: authenticatable.id }
  authenticatable
end

#sign_out(authenticatable_class) ⇒ boolean

Signs out user by deleting their encrypted cookie.

Parameters:

  • authenticatable_class (ActiveRecord::Base)

    any Model connected to passwordless. (e.g - User or Admin).

Returns:

  • (boolean)

    Always true



48
49
50
51
52
53
# File 'lib/passwordless/controller_helpers.rb', line 48

def sign_out(authenticatable_class)
  key = cookie_name(authenticatable_class)
  cookies.encrypted.permanent[key] = { value: nil }
  cookies.delete(key)
  true
end