Class: MagicLinks::Middleware::MagicTokenRedirect::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/magic_links/middleware/magic_token_redirect.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Handler

Returns a new instance of Handler.



17
18
19
# File 'lib/magic_links/middleware/magic_token_redirect.rb', line 17

def initialize(env)
  @request = ActionDispatch::Request.new(env)
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



33
34
35
# File 'lib/magic_links/middleware/magic_token_redirect.rb', line 33

def request
  @request
end

Instance Method Details

#handleObject



21
22
23
24
25
26
27
# File 'lib/magic_links/middleware/magic_token_redirect.rb', line 21

def handle
  return unless redirect_request?
  return root unless magic_token.present?

  cookies.encrypted[magic_token_key] = {value: magic_token.token, expires: cookie_expiry} if scope
  respond_with_redirect magic_token.target_path
end

#magic_tokenObject



43
44
45
46
47
# File 'lib/magic_links/middleware/magic_token_redirect.rb', line 43

def magic_token
  return unless token

  @magic_token ||= MagicToken.find_by(token: token)
end

#pathObject



35
36
37
# File 'lib/magic_links/middleware/magic_token_redirect.rb', line 35

def path
  request.path
end

#redirect_request?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/magic_links/middleware/magic_token_redirect.rb', line 39

def redirect_request?
  Templates.match?(path)
end

#rootObject



29
30
31
# File 'lib/magic_links/middleware/magic_token_redirect.rb', line 29

def root
  respond_with_redirect '/', 'to the home page (token not found)'
end

#scopeObject



53
54
55
# File 'lib/magic_links/middleware/magic_token_redirect.rb', line 53

def scope
  magic_token&.scope
end

#tokenObject



49
50
51
# File 'lib/magic_links/middleware/magic_token_redirect.rb', line 49

def token
  @token ||= Templates.token_for(path)
end