0

I've already did the google sign with the firebase flutter toolkit. When sign in is done, I receive the idToken to read/write on the database.

final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn googleSignIn = GoogleSignIn();

Future<String> signInWithGoogle() async {
  await Firebase.initializeApp();

  final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
  final GoogleSignInAuthentication googleSignInAuthentication = await googleSignInAccount.authentication;

  final AuthCredential credential = GoogleAuthProvider.credential(
    accessToken: googleSignInAuthentication.accessToken,
    idToken: googleSignInAuthentication.idToken,
  );

  final UserCredential authResult = await _auth.signInWithCredential(credential);
  final User user = authResult.user;

  final idToken = await user.getIdToken();
}

The problem is, I need to auto login user when he opens the app again. So, when I sign in with google, I do not receive the refresh token, needed to get the new valid idToken, as the Doc.

How can I get the refreshToken ?

Marcello Câmara
  • 187
  • 1
  • 13
  • The Firebase Auth SDK will automatically sign in the user if they previously signed in. There should be no need for a refresh token. Please edit the question to explain in more detail what is the problem you're trying to solve here. – Doug Stevenson Dec 10 '20 at 17:42
  • @DougStevenson I'm using firebase rest api (I'm not using this auto login that you told), so I need the refresh token as told in first post to renew the idToken with the refreshToken (see the doc then I think you will understand). The refreshToken is needed to Exchange a refresh token for an ID token – Marcello Câmara Dec 11 '20 at 01:33
  • I don't see any evidence that you're using a REST API here. I see you're using the Firebase Auth SDK using `_auth.signInWithCredential()`. The token from that signin will be refreshed automatically. There is nothing else you have to do to enable this. – Doug Stevenson Dec 12 '20 at 07:35

2 Answers2

0

You can use SharedPreferences to set a bool value when a user is logged in from google and check the value when the app restarts. Don't forget to remove this value from shared preferences when user logged out.

Ranjit Shrestha
  • 622
  • 6
  • 24
  • I'm using sharedPreferences to save the refreshToken and idToken logged in with email and password. But that's not the case. On google sign in I do not receive the refreshToken. – Marcello Câmara Dec 11 '20 at 01:35
  • @MarcelloCâmara were you able to figure this out? I am also stuck. I even impelemented this: https://stackoverflow.com/questions/62230448/obtain-a-new-token-by-refresh-token-with-google-sign-in-flutter but it doesn't give me a refresh token. – mcfred Mar 08 '21 at 06:54
  • Yes @bangbang I've implemented the refresh token for REST API. But when user is logged in with a Google account, you can't (and don't need to) use the refresh token. You must verify if a google account is logged in via `FirebaseAuth.instance.currentUser`. If there's an user, the idToken is valid and you can use it by `await googleUser.getIdToken(true)`. They are two different fluxes, one for REST API and another for the firebase auth (that your logged user via Google Sign In and you will not need the refresh token) – Marcello Câmara Mar 08 '21 at 16:39
0

I'm pretty sure googl_sign_in keeps the refresh token internally and you always receive a valid idToken. If it's about to expire, you can force renewal.

Answering your direct question about refreshToken:

You can create your own implementation of the google_sign_in class. In the end it's just an OAuth token. Here's a web implementation of a custom google_sign_in service: https://stackoverflow.com/a/69746036/5492496

I'm pretty sure you can do the same for Android & iOS using web view, you will loose the single click functionality on Android though.

RMK
  • 1,897
  • 1
  • 10
  • 17