Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: SetAuthTokenFunc() #615

Open
MB175 opened this issue Feb 8, 2023 · 5 comments
Open

Feature: SetAuthTokenFunc() #615

MB175 opened this issue Feb 8, 2023 · 5 comments

Comments

@MB175
Copy link

MB175 commented Feb 8, 2023

Add ability to pass a custom func which is called on every request to renew or check authToken validity

@kecci
Copy link

kecci commented Feb 20, 2023

Hi @MB175,

I think you can setup the middleware for every request:

  • Use client func OnBeforeRequest to renew auth token
  • Use client func OnAfterResponse to check auth token validity
// Create a Resty Client
client := resty.New()

// Registering Request Middleware
client.OnBeforeRequest(func(c *resty.Client, req *resty.Request) error {
    // Now you have access to Client and current Request object
    // manipulate it as per your need

    return nil  // if its success otherwise return error
  })

// Registering Response Middleware
client.OnAfterResponse(func(c *resty.Client, resp *resty.Response) error {
    // Now you have access to Client and current Response object
    // manipulate it as per your need

    return nil  // if its success otherwise return error
  })

Source: https://github.com/go-resty/resty#request-and-response-middleware

I hope this could be help this discussion.

@MB175
Copy link
Author

MB175 commented Feb 20, 2023

@kecci hi, yes thats exactly how I solved it right now.
Thanks for your reply anyway, appreciate it.

I just thought since there is a set auth token func, it would be just as handy to have a dedicated refresher func.

@jeevatkm
Copy link
Member

jeevatkm commented Mar 6, 2023

@MB175 Thanks for reaching out and proposing a new feature. I will consider this for Resty v3.

Thanks @kecci for your help and assisting the fellow Resty user.

@1Ckpwee
Copy link

1Ckpwee commented Apr 5, 2023

Hi @MB175,
According to @kecci answer we can know:
We can setup the middleware for every request to do renew auth token
Maybe I think your question is how to refresh or verify the auth token validity in this way.
And if so, we can refer to the golang/oauth2 implementation to validate and refresh auto token for each request in ourself defined request middleware.

Yes, as quoted above I think we should check that the token has expired before refreshing the token.

@MB175
Copy link
Author

MB175 commented Apr 5, 2023

Yes that's the setup I use, it's not about what refresh cycle or method is used. It's more about a convince Feature since there is a method to set an token I figured it might make sense if you could pass a custom refresher func along side

@jeevatkm jeevatkm added this to the v3.0.0 Milestone milestone Sep 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

4 participants