Class: Airwallex::Middleware::AuthRefresh
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- Airwallex::Middleware::AuthRefresh
- Defined in:
- lib/airwallex/middleware/auth_refresh.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, client) ⇒ AuthRefresh
constructor
A new instance of AuthRefresh.
Constructor Details
#initialize(app, client) ⇒ AuthRefresh
Returns a new instance of AuthRefresh.
6 7 8 9 |
# File 'lib/airwallex/middleware/auth_refresh.rb', line 6 def initialize(app, client) super(app) @client = client end |
Instance Method Details
#call(env) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/airwallex/middleware/auth_refresh.rb', line 11 def call(env) # Skip authentication entirely for the login endpoint itself return @app.call(env) if login_request?(env) # Ensure token is valid before making the request, then attach it @client.ensure_authenticated! unless authentication_request?(env) (env) response = @app.call(env) # If we get a 401, try refreshing the token and retrying once if response.status == 401 @client.authenticate! (env) response = @app.call(env) end response end |