-
Notifications
You must be signed in to change notification settings - Fork 64
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
Comments
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. |
You can create an This an example of a 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 final hasuraConnect = HasuraConnect(
url,
headers: headers, // use it for constant headers
interceptors: [TokenInterceptor(auth)],
); |
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 |
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. |
Here's my workaround if anyone is interested: https://gist.github.com/osaxma/141d6be2b522f8bfe8673af14eb20bd1 |
Thank you @osaxma. I will look into your workaround later and come back with comments/feedback. |
Isn't that issue the same as #67? |
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.
The text was updated successfully, but these errors were encountered: