-
Notifications
You must be signed in to change notification settings - Fork 122
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
Updating Firebase token for client #12
Comments
Hey @awaik, I'm not too familiar with how Firebase updates the token, but I can assume it's at the network request level. If so, we can narrow your issue down to the link instead of the whole client. The problem is that we might not have implemented an appropriate link for your use-case. Can you describe how the communication occures at the network level? What's the normal traffic; what happens when the token expires; what Firebase expects you to do next? |
Hello @klavs, I try to explain, please ask more if I miss something :) Firstly user logged in and get:
Next, for making requests to functions or database we get
As Google guys offer to work with this tokens here https://firebase.google.com/docs/auth/admin/manage-sessions we should check and revoke token in case it outdated. For example, in Flutter we can check it like this
So, in our app we have states:
And every 3600 seconds idToken become outdated on the server-side. |
in addition to the previous post - so, we should use the link with token and have possibility to update it globally anytime in the app
|
@awaik at this moment there isn't a built-in way to retry requests if the previous request has failed with an auth error. Here you can take a look at an example how a context can be passed along a request to use custom headers. You can take a look at the Transform link which can be used to add a context entry on every request. https://github.com/gql-dart/gql/tree/master/gql_transform_link |
@klavs Thank you for pointing out. For those who will look for decision, made this code:
|
How would I add a header if I am manually getting the stream or result without using a the query widget |
Options seem missing in the client |
I think this can be easily taken care y the httpClient option that gql_http_link package provides. FirebaseAuth does provide a stream where you can listen for the changes in id token. You can use this stream subscription and make the appropriate changes before the request is even fired. Here is an example: class _HttpClient extends http.BaseClient {
_HttpClient._() {
startIdTokenListener();
}
static final _instance = _HttpClient._();
final http.Client _client = http.Client();
String _token;
StreamSubscription idTokenSubscription;
static _HttpClient get instance => _instance;
Future<String> _getTokenFuture;
Future<http.StreamedResponse> send(http.BaseRequest request) async {
if (_getTokenFuture != null) {
await _getTokenFuture;
}
// log.d("Bearer $_token");
request.headers['Authorization'] = "Bearer $_token";
return _client.send(request);
}
void startIdTokenListener() {
idTokenSubscription = FirebaseAuth.instance.idTokenChanges().listen(idTokenChangeListener);
}
@override
void close() {
idTokenSubscription?.cancel();
_client.close();
super.close();
}
void idTokenChangeListener(User firebaseUser) async {
if (firebaseUser == null) {
if (_token != null) {
_token = null;
}
return;
}
_getTokenFuture = firebaseUser.getIdToken();
_token = await _getTokenFuture;
_getTokenFuture = null;
}
} and you can use this http client in the HttpLink constructor like this final link = HttpLink(
"your-graphql-link",
httpClient: _HttpClient.instance,
); Maybe @klavs or @smkhalsa could let us know whether using the httpClient option for intercepting the requests can cause any side effects. |
For future gitters who stumbled upon this issue, you can refer to this code https://github.com/zino-app/graphql-flutter/blob/beta/packages/graphql/lib/src/links/auth_link.dart , and use it like:
Adding the package to pubspec is not possible since at the time I write this there's a dependency mismatch between graphql and ferry regarding their dependency on rxdart.
|
Hello, I've started implementing your package and have difficulties with updating the client token for Firebase.
They issue a new token every 3600 seconds. So, I have to have a reliable way to update it in the app.
Right now you in your example you suggest using such an approach
I tried to make reliable updating for connecting with the short living token, but I failed. If you have some best practices for this, can you please share?
Maybe, it could be useful to add update() method to Client
Thank you for such a nice plugin!
The text was updated successfully, but these errors were encountered: