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

Subscriptions stop working after the token expires. #84

Open
develogo opened this issue Jan 6, 2021 · 8 comments
Open

Subscriptions stop working after the token expires. #84

develogo opened this issue Jan 6, 2021 · 8 comments

Comments

@develogo
Copy link

develogo commented Jan 6, 2021

Subscriptions stop working after the token expires. Even generating a new token and setting it as a new token, the subs do not work again.

@andreyeurope
Copy link

I am using hasura_connect myself, and I have seen this happening too, especially when your authentication header is based on firebase auth, and the id token expires.
Would be nice to be able to catch this kind of error, or have hasura_connect handle them internally when the error happens.

@osaxma
Copy link

osaxma commented Jan 20, 2021

You can create an Interceptor instead of using the header argument in HasuraConnect. The interceptor can inject the token header into the request headers in every request with the latest JWT from your auth provider.

This an example of aTokenInterceptor using firebase taken from the readme with minor modifications:

class TokenInterceptor extends Interceptor {
  final FirebaseAuth auth;
  TokenInterceptor(this.auth);

  @override
  Future<void> onConnected(HasuraConnect connect) {}

  @override
  Future<void> onDisconnected() {}

  @override
  Future onError(HasuraError request) async {
    return request;
  }

  @override
  Future<Request> onRequest(Request request) async {
    var user = await auth.currentUser();
    var token = await user.getIdToken(); // firebase will return unexpired token (it takes care of the checking) 
    if (token != null) {
      try {
        request.headers["Authorization"] = "Bearer ${token.token}";
        return request;
      } catch (e) {
        return null;
      }
    } else {
    // do something if the token is null 
    }
  }

  @override
  Future onResponse(Response data) async {
    return data;
  }

  @override
  Future<void> onSubscription(Request request, Snapshot snapshot) {}

  @override
  Future<void> onTryAgain(HasuraConnect connect) {}
}

Now when you initialize the client, pass your TokenInterceptor like this:

final hasuraConnect = HasuraConnect(
    url,
    headers: headers, // use it for constant headers 
    interceptors: [TokenInterceptor(auth)],
  );

@andreyeurope
Copy link

andreyeurope commented Jan 20, 2021

Thank you @osaxma , but this approach doesn't work for subscriptions, because this is what I use for my hasura connect instance.

It works just fine until the token expires. When it expired, Hasura will send something like connection not authorized or similar (tested with Apollo in React), but nothing happens in hasura connect packages.

A solution would be to throw an exception or something to let us (the users of the library) know that you have to open a new connection or do something about the current connection, or internally call again the onRequest method .

@osaxma
Copy link

osaxma commented Jan 20, 2021

I totally missed that part. By the way, I'm not a maintainer here and I'm a user too... I just came across the issue few days ago and I thought that an interceptor would solve it but apparently not.. Thank you for clarifying that.

@osaxma
Copy link

osaxma commented Feb 8, 2021

Here's my workaround if anyone is interested:

https://gist.github.com/osaxma/141d6be2b522f8bfe8673af14eb20bd1

@andreyeurope
Copy link

Thank you @osaxma. I will look into your workaround later and come back with comments/feedback.

@evandrmb
Copy link

Isn't that issue the same as #67?

@andreyeurope
Copy link

Isn't that issue the same as #67?

Yeah, I think it is the same. Probably we can close this one and use #67

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

No branches or pull requests

4 participants