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

Firebase Token Expires every hour, subscriptions stop working #67

Open
arpitjacob opened this issue Jul 28, 2020 · 11 comments
Open

Firebase Token Expires every hour, subscriptions stop working #67

arpitjacob opened this issue Jul 28, 2020 · 11 comments
Assignees
Labels
bug Something isn't working

Comments

@arpitjacob
Copy link

Hi there, I am using firebase to validate the JWT. but it expires every one hour.
Where should I put a condition in the code to get a new token every time it expires
I also have subscriptions, so I am getting "Try again..." repeated in the logs every 2 seconds when the token expires.

@rajababu3
Copy link

You can create a client something like this.

class HasuraClient {
  HasuraConnect get hasuraConnect =>
      HasuraConnect("https://<your-url>/v1/graphql", headers: {
        "X-Hasura-Role": "user",
      }, token: (isError) async {
        //sharedPreferences or other storage logic
        final currentUser = await UserRepository().getCurrentUser();
        final idToken = await currentUser.getIdToken(refresh: true);
        return "Bearer ${idToken.token}";
      });
}

@arpitjacob
Copy link
Author

Thanks I was using exactly this code and I had removed refresh : true from getIdToken(refresh: true).

I think I did it because it's called on every query. Does it not make all the queries slower to process if it does a force refresh? or does it return only when a new token is available?

@rajababu3
Copy link

rajababu3 commented Aug 6, 2020 via email

@arpitjacob
Copy link
Author

Ah ok the documentation on firebase is a bit confusing then. I need to read it again.

@arpitjacob
Copy link
Author

This doesn't work for subscriptions. I left the app open for more than an hour. I am still seeing this error.

flutter: Try again...

@Bwolfs2 Bwolfs2 added the bug Something isn't working label Aug 7, 2020
@rajababu3
Copy link

@arpitjacob
Copy link
Author

Link doesn't work anymore, I don't think it check for token expiration its only checks if token in not null. So you can have a valid token which has expired.

@arpitjacob
Copy link
Author

Hi guys is there any solution to this or a temporary fix I can use till you guys send out an update?

@Bwolfs2
Copy link
Member

Bwolfs2 commented Nov 23, 2020

If u are using 2.0+ u will need to implement a auth_interceptor to make this:

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(refresh: true);
    if (token != null) {
      try {
        request.headers["Authorization"] = "Bearer ${token.token}";
        return request;
      } catch (e) {
        return null;
      }
    } else {
      Modular.to.pushReplacementNamed("/login");
    }
  }

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

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

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

@osaxma
Copy link

osaxma commented Jan 20, 2021

@Bwolfs2

How to make this work with an ongoing subscription (i.e. when the token expires in the middle of an active subscription)?

@osaxma
Copy link

osaxma commented Feb 8, 2021

Here's my workaround if anyone is interested:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants