Skip to content

Conversation

@voytech-net
Copy link
Contributor

@voytech-net voytech-net commented Apr 8, 2025

@agordn52
When token needs to be refreshed the overridden methods are correctly used, however the request that triggers this check fails, since it was called with the old expired token.

Proposed change:

  • add another method that could be overridden that would return a new fresh token (from API, Backpack, Storage, anywhere)
  • use this token instead of the old bearer token
  • this way it should not be a breaking change

@voytech-net
Copy link
Contributor Author

@agordn52 Hi, is this something worth merging? what are your thoughts? much appreciated

@agordn52
Copy link
Collaborator

Hi @voytech-net,

Sorry for the late response!
What would be the difference to using setAuthHeaders?

@override
  Future<RequestHeaders> setAuthHeaders(RequestHeaders headers) async {
    String? token = Backpack.instance.read('MY_TOKEN');

    if (user != null) {
      headers.addBearerToken( token );
    }
    return headers;
  }

@voytech-net
Copy link
Contributor Author

@agordn52 Yup, I am using this, but from my understanding of the flow:

if (await shouldRefreshToken()) { <--- I need to refresh the token, it is expired
        await refreshToken(Dio()); <--- I have refreshed it, yay!
      }
      if (bearerToken != null) { <--- this bearer token is the old expired one
        newValuesToAddToHeader.addAll({"Authorization": "Bearer $bearerToken"});
      } else {
        if ((shouldSetAuthHeaders ?? this.shouldSetAuthHeaders) == true) { 
          newValuesToAddToHeader.addAll(await setAuthHeaders(headers)); <--- this does not get called since the bearerToken is not null
        }
      }

Is my understanding correct, or this works already and no futher action is required?

@agordn52
Copy link
Collaborator

@agordn52 Yup, I am using this, but from my understanding of the flow:

if (await shouldRefreshToken()) { <--- I need to refresh the token, it is expired
        await refreshToken(Dio()); <--- I have refreshed it, yay!
      }
      if (bearerToken != null) { <--- this bearer token is the old expired one
        newValuesToAddToHeader.addAll({"Authorization": "Bearer $bearerToken"});
      } else {
        if ((shouldSetAuthHeaders ?? this.shouldSetAuthHeaders) == true) { 
          newValuesToAddToHeader.addAll(await setAuthHeaders(headers)); <--- this does not get called since the bearerToken is not null
        }
      }

Is my understanding correct, or this works already and no futher action is required?

@voytech-net, yeah kinda :)

So, if you're using the network helper inside your ApiService and pass a bearerToken it will override the one you setting using the setAuthHeaders helper.

Future fetchData() async {
    return await network(
      request: (request) =>
          request.get("/my-endpoint"),
      // bearerToken: "" // You won't need this one ❌
    );
  }

...

@override
Future<RequestHeaders> setAuthHeaders(RequestHeaders headers) async {
    String? myAuthToken = await Keys.bearerToken.read();
    if (myAuthToken != null) {
      headers.addBearerToken( myAuthToken );
    }
    return headers;
}

Give it a try 👍
If you still need some custom requirement though, lets hop on a quick Google Meet call to discuss it

@voytech-net
Copy link
Contributor Author

Ok, understood. No issues then. Closing this. Thanks! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants