1

I am having trouble getting the Firebase auth stream to work for a token refresh. I listen to a stream, and upon a token change, such as a token refresh, I want to trigger a function that updates the client link with the new headers. I need the header to pass authorization rules on the backend.

To do so, I set up a stream in my root widget's initState that fires when the app is first built and set up the listener by reaching out to an AuthService class that then triggers a function in the Api class that updates the client. When I manually call currentUser.getIdToken(true) to refresh the token, the streams fire, but not when the token is automatically refreshed after an hour. I wonder whether I somehow turned off the auto-refresh function, or my listeners aren't set up correctly, or whether there is something else I am missing.

Setting up the listeners:

@override
  void initState() {
    locator<AuthService>().auth.idTokenChanges().listen((user) {
      user!
          .getIdToken()
          .then((token) => locator<DgraphApi>().setNewToken(token));
    });

Updating the client:

late GraphQLClient client;

void setNewToken(String token) {
    HttpLink httpLink = HttpLink(
      dotenv.env['DGRAPH_ENDPOINT']!,
      defaultHeaders: {'X-Auth-Token': token, 
      'DG-Auth': dotenv.env['KEY']!
      }
      ,
    );
    client = GraphQLClient(
      cache: GraphQLCache(),
      link: httpLink,
    );
  }
alexn62
  • 51
  • 7

0 Answers0