Class: Userbin::Request::Middleware::SessionToken

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/userbin/request.rb

Overview

Sends the active session token in a header, and extracts the returned session token and sets it locally.

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/userbin/request.rb', line 80

def call(env)
  userbin = RequestStore.store[:userbin]
  return @app.call(env) unless userbin

  # get the session token from our local store
  if userbin.session_token
    env[:request_headers]['X-Userbin-Session-Token'] =
      userbin.session_token.to_s
  end

  # call the API
  response = @app.call(env)

  # update the local store with the updated session token
  token = response.env.response_headers['x-userbin-set-session-token']
  userbin.session_token = token if token

  response
end