Class: Clerk::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/clerk/rack_middleware.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Proxy

Returns a new instance of Proxy.



18
19
20
21
22
23
24
25
# File 'lib/clerk/rack_middleware.rb', line 18

def initialize(env)
  req = Rack::Request.new(env)
  @token = req.cookies["__session"]
  @session_id = req.params["_clerk_session_id"]
  @session = nil
  @user_id = nil
  @user = nil
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



17
18
19
# File 'lib/clerk/rack_middleware.rb', line 17

def error
  @error
end

#session_idObject (readonly)

Returns the value of attribute session_id.



17
18
19
# File 'lib/clerk/rack_middleware.rb', line 17

def session_id
  @session_id
end

Instance Method Details

#debugObject



48
49
50
51
52
# File 'lib/clerk/rack_middleware.rb', line 48

def debug
  (instance_variables - [:@sdk]).map do |ivar|
    [ivar.to_s, instance_variable_get(ivar)]
  end.to_h
end

#sessionObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/clerk/rack_middleware.rb', line 27

def session
  return nil if @token.nil?
  return @session if @session

  begin
    @session = fetch_session
  rescue Errors::Authentication => e
    @error = e
  end
  @session
end

#userObject



43
44
45
46
# File 'lib/clerk/rack_middleware.rb', line 43

def user
  return nil if session.nil?
  @user ||= fetch_user(user_id)
end

#user_idObject



39
40
41
# File 'lib/clerk/rack_middleware.rb', line 39

def user_id
  @user_id ||= session.dig("user_id")
end