Flutter Google Sign In idToken expired - flutter

I am using google_sign_in for auth in my flutter application.
I followed steps documented here to get started:
https://github.com/flutter/plugins/blob/master/packages/google_sign_in/google_sign_in/example/lib/main.dart
I get idToken as recommended to perform auth on the server side.
This is the code snippet I use to get idToken
_googleSignIn.onCurrentUserChanged.listen(
(GoogleSignInAccount account) {
setState(() {
developer.log("************Changing account");
_currentUser = account;
account.authentication.then((key){
developer.log("########### Changing token");
_userToken=key.idToken;
});
});
});
When I send idToken (_userToken in above example) it works fine for sometime, and later I see the following error message on the server side:
Token expired, 1593370077 < 1593384191
IT looks like token has an expiration time and expires after a while.
I tried moving above code snippet from initState() to build(). Here my assumption if I call it everytime when I am building the widget I should get a new token. THAT DIDN'T WORK
Reason I thought it might work was because when I "Run .dart" (not hot reload) it works. This made me assume moving it to build might work . It didn't work
My question: What is the programmatic way to refresh token, or get a new token in flutter using google_sign_in.
Thanks for your help!!

The idToken expires every 30 minutes.
To refresh the token you can either user the API or do a silent login like this:
Future<String> refreshToken() async {
final GoogleSignInAccount googleSignInAccount =
await googleSignIn.signInSilently();
final GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
await auth.signInWithCredential(credential);
return googleSignInAuthentication.accessToken; //new token
}

Related

Flutter Google Sign In Keeps Asking for Permission

I am working with the Google Sign In auth through Firebase but each time I test the app in iOS Google keeps asking for permissions whenever I login with Google Sing In. In Android it only asks for permission once.
Has anyone encountered a similar issue?
This is my Google Sign In code:
class AuthService {
signInWithGoogle() async {
final GoogleSignInAccount? gUser = await GoogleSignIn().signIn();
final GoogleSignInAuthentication gAuth = await gUser!.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: gAuth.accessToken,
idToken: gAuth.idToken,
);
await FirebaseAuth.instance.signInWithCredential(credential);
}
}
I tried to find another code but none of them worked!
Please make sure that you have enabled Google authentication in firebase and is using the updated google-services.json file and GoogleService-Info.plist files.
You can make use of google_sign_in package.
Also, please add the scope when instantiating Google signin
GoogleSignIn googleSignIn = GoogleSignIn(
scopes: [
'email',
],
);
GoogleSignInAccount? gAccount = await googleSignIn.signIn();
final credential = await gAccount!.authentication;

Error when trying to authenticate users with google in firebase

I've been struggling with this for months, sinc.e I'm just learning firebase with flutter.
I have a simple button that when pressed should give the option to register or start section with google, but when running I get errors in the console.
There are so many that I don't know which one it is.
Error: Assertion failed:
file:///C:/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_core_web-1.7.2/lib/src/firebase_core_web.dart:207:11
options != null
"FirebaseOptions cannot be null when creating the default app."
Error: [core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()
here in onpress of the button:
child: MaterialButton(
onPressed: () async {
User? user = await Authenticator.IniciarSesion(context: context);
print(user?.displayName);
},
and the class that authenticates
class Authenticator {
static Future<User?> IniciarSesion({required BuildContext context}) async {
FirebaseAuth authenticator = FirebaseAuth.instance;
User? user;
GoogleSignIn objGooglesignIn = GoogleSignIn();
GoogleSignInAccount? objGooglesignInAccount =
await objGooglesignIn.signIn();
if (objGooglesignInAccount != null) {
GoogleSignInAuthentication objGoogleSignInAuthentication =
await objGooglesignInAccount.authentication;
AuthCredential credential = GoogleAuthProvider.credential(
accessToken: objGoogleSignInAuthentication.accessToken,
idToken: objGoogleSignInAuthentication.idToken);
try {
UserCredential userCredential =
await authenticator.signInWithCredential(credential);
user = userCredential.user;
return user;
} on FirebaseAuthException catch (e) {
print("error de auth");
}
}
}
}
Sorry if I put a lot of code. I don't know exactly what the error could be.
There are two issues with your application, first one seems like you are not initializing firebase in the main function properly. Please refer mentioned link for setup.
Flutter firebase setup guide
Additionally as I can see there is no firebase_options.dart in your lib directory which basically manages (api key) configurations for different platforms.
If you have followed steps mentioned in guide correctly you can create these configs using
flutterfire configure
command only after you have already installed flutterfire cli using following command
dart pub global activate flutterfire_cli

Google Sign in returns to login after account selection

Google Sign in is working on ios (published) but doesn't work in release on Android. On hitting the button the Sign in process is started and you can select an account but after selection of the right account it just return to the login.. nothing happens... However on my emulator it does work. -_-`.
I checked SHA-1 and SHA-256 (release-keys) and updated the google-service.json file.
signWithGoogle() async {
final googleSignin = GoogleSignIn(scopes: ['email']);
final GoogleSignInAccount? googleUser =
await googleSignin.signIn();
final GoogleSignInAuthentication googleAuth =
await googleUser!.authentication;
final GoogleAuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
) as GoogleAuthCredential;
final res = await FirebaseAuth.instance.signInWithCredential(credential);
await FirebaseFirestore.instance.collection('Users').doc(res.user!.uid).set({
'name' : res.user!.displayName,
'email': res.user!.email,
'uid': res.user!.uid,
'image': res.user!.photoURL,
}, SetOptions(merge : true));
return res.user;
}
I'm using google_sign_in package.
There are 2 sets of sha keys. When you upload an app bundle google signs an apk. This key needs to be updated in firebase.
You have to copy keys from app sign in key section. The release jks's keys is named here as upload key certificate.. use the app sign in key's sha 1 and sha256 in firebase and download the json. Add it to the app and reupload. This should work as expected

How to get refresh token and expiresInSeconds from flutter Google Sign In plugin

I'm trying to implement the Google Sign In into my flutter application, but i'm missing some information :
- refresh Token
- ExpiresInSeconds.
How are you able to get this information?
GoogleSignIn _googlSignIn = new GoogleSignIn(scopes: [
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/youtube',
]);
print('Google user ==>');
final GoogleSignInAccount googleUser = await _googlSignIn.signIn();
print(googleUser.id); //account_uid
print(googleUser.email);
print(googleUser.authHeaders);
print(googleUser.displayName); //display_name
print(googleUser.photoUrl); //image_url
print('Google Auth ==>');
final GoogleSignInAuthentication googleAuth = await googleUser.authentication;
print(googleAuth.accessToken);
print(googleAuth.hashCode);
print(googleAuth.idToken);
I need to specify the following items as well,
include_granted_scopes : true
access_type: offline
Generating a refresh token is yet to be supported by the google_sign_in plugin. A workaround was posted in this issue thread. A refresh token can be manually generated by sending out a request to Identity Platform API.

How can you log into google oauth with a preset email and password?

I'm working on a flutter app for a client.
This involves uploading files to a Google drive folder.
I've got it working with the Google sign in pop-up, but they want it so that the account used is separated from the device accounts.
Is this possible?
I don't think service accounts are right because they still want the email and password to be set in the app settings, and from what I understand service accounts need to be made and linked directly in the API console.
EDIT: here's my current code for logging in with the Google sign-in plugin;
static final GoogleSignIn _googleSignIn = GoogleSignIn(
scopes: [
g.DriveApi.DriveScope,
g.DriveApi.DriveMetadataReadonlyScope
]
);
static dynamic token;
...
static login() async{
try {
final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
final GoogleSignInAuthentication googleAuth = await googleUser
.authentication;
print("signed in " + googleAuth.accessToken);
token = googleAuth.accessToken;
_authHeaders = await _googleSignIn.currentUser.authHeaders;
}catch(exception){
//maybe offline
print("login error: "+exception.toString());
}
}
So is it possible to use email and password instead of this method (which show's an account selection pop-up)?