PlatformException(popup_blocked_by_browser, Exception raised from GoogleAuth.signIn() - flutter

I keep on getting
PlatformException(popup_blocked_by_browser, Exception raised from GoogleAuth.signIn() every first attempt when trying to use google_sign_in flutter package to sign in via Google. This only happens when I use safari, its fine on chrome.
I'm wondering why it fails on the first attempt but works on the attempts after. And if I refresh my browser it fails again on the first attempt.
Here's my function using firebase auth and google sign in
#override
Future<auth.User?> signInWithGoogle() async {
final GoogleSignInAccount? googleUser =
await _googleSignIn.signInSilently();
final GoogleSignInAuthentication? googleAuth =
await googleUser?.authentication;
if (googleAuth == null) {
return null;
}
final AuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
final UserCredential userCredential =
await _firebaseAuth.signInWithCredential(credential);
return auth.User(
uid: userCredential.user?.uid,
email: userCredential.user?.email,
);
}

Related

Flutter Google sign-in error on first time login

I am using google_sign_in plugin. It's running fine if the user has already account logged in and user just needs to select his account.
But when a new google account is added just before the login, then googleUser remains null and hence it throws exception.
Here's the code.
Future<UserCredential> signInWithGoogle() async {
//here googleUser remains null on first time login.....
GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();
// Obtain the auth details from the request
final GoogleSignInAuthentication? googleAuth =
await googleUser?.authentication;
final OAuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleAuth?.accessToken,
idToken: googleAuth?.idToken,
);
// Once signed in, return the UserCredential
return await FirebaseAuth.instance.signInWithCredential(credential);
}

Flutter Platform Exception in Google Sign

I have successfully completed the Google sign-in thing in Flutter. But if the user quits logging in I get an error. I couldn't figure out how to check for these errors. The answers I found are very old. Here is my code.
Future<UserCredential> signInWithGoogle() async {
final GoogleSignInAccount? googleUser =
await GoogleSignIn(scopes: <String>["email"]).signIn();
final GoogleSignInAuthentication googleAuth =
await googleUser!.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
return await FirebaseAuth.instance.signInWithCredential(credential);
}
And my error
Exception has occurred.
PlatformException (PlatformException(sign_in_canceled, com.google.GIDSignIn, The user canceled the sign-in flow., null))
How can i handle that?

How can i get the access token through google sign in -- Flutter

I want to get the access token from google sign and save it as a variable so I'll be able to push it to my own API , this question has been asked before but the answers are outdated . I'm using this package :Google sign in
her,s my log in code that i access through a button :
GestureDetector(
onTap: () {
_googleSignIn.signIn().catchError((e) {
print('Erorr');
});
;
},
To get access token when logged in with google use
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth?.accessToken,
idToken: googleAuth?.idToken,
);
Full method:
import 'package:google_sign_in/google_sign_in.dart';
Future<UserCredential> signInWithGoogle() async {
// Trigger the authentication flow
final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();
// Obtain the auth details from the request
final GoogleSignInAuthentication? googleAuth = await googleUser?.authentication;
// Create a new credential
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth?.accessToken,
idToken: googleAuth?.idToken,
);
// Once signed in, return the UserCredential
return await FirebaseAuth.instance.signInWithCredential(credential);
}
for document https://firebase.flutter.dev/docs/auth/social/
Future<bool> google() async {
try {
final googleUser = await googleSignIn.signIn();
final googleAuth = await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleAuth?.accessToken,// accessToken
idToken: googleAuth?.idToken,
);
users = (await auth.signInWithCredential(credential)).user;
if (users == null) {
return false;
}
return true;
} catch (e) {
print('this is error .......$e');
return null;
}
you can get the access token
googleAuth.accessToken //fellowing my code
Future<FirebaseUser> signInWithGoogle(SignInViewModel model) async {
model.state =ViewState.Busy;
GoogleSignInAccount googleSignInAccount = await _googleSignIn.signIn();
GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;
AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
AuthResult authResult = await _auth.signInWithCredential(credential);
_user = authResult.user;
assert(!_user.isAnonymous);
assert(await _user.getIdToken() != null);
FirebaseUser currentUser = await _auth.currentUser();
assert(_user.uid == currentUser.uid);
model.state =ViewState.Idle;
print("User Name: ${_user.displayName}");
print("User Email ${_user.email}");
}

flutter how to catch error google sign in

Future<UserCredential> signInWithGoogle() async {
// Trigger the authentication flow
final GoogleSignInAccount googleUser = await GoogleSignIn().signIn();
// Obtain the auth details from the request
final GoogleSignInAuthentication googleAuth =
await googleUser.authentication;
// Create a new credential
final GoogleAuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
// Once signed in, return the UserCredential
return await FirebaseAuth.instance.signInWithCredential(credential);
}
Future signInWithFacebook() async {
// Trigger the sign-in flow
try {
final AccessToken accessToken = await FacebookAuth.instance.login();
// Create a credential from the access token
final OAuthCredential credential = FacebookAuthProvider.credential(
accessToken.token,
);
// Once signed in, return the UserCredential
return await FirebaseAuth.instance.signInWithCredential(credential);
} on FacebookAuthException catch (e) {
// handle the FacebookAuthException
} on FirebaseAuthException catch (e) {
// handle the FirebaseAuthException
} finally {}
return null;
}
//pub.yaml
google_sign_in: ^5.0.1
make the social login auth but I can't catch error google sign in.
I'm already Facebook sign in and apple sign in error catch but
I can't catch error on google sign in
please help me
If you make the return type of <UserCredential?> nullable, you'd be able to work with it.
//this UserCredential?
Future<UserCredential?> signInWithGoogle() async {
// Trigger the authentication flow
try {
final GoogleSignInAccount? googleUser = await (GoogleSignIn().signIn());
// Obtain the auth details from the request
final GoogleSignInAuthentication googleAuth = await googleUser!.authentication;
// Create a new credential
final GoogleAuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
) as GoogleAuthCredential;
// Once signed in, return the UserCredential
return await FirebaseAuth.instance.signInWithCredential(credential);
} catch (e) {
print(e);
return null ;
}
}
Right now I'm also facing this issue. Your code is fine. Run your app in Release mode and everything will work fine as expected. It's very strange behavior.
this only occurs in Emulators, not in the real devices.

Make flutter app force a user to choose an account with FirebaseAuth and GoogleSignInAuthentication

I want to force a user to select one of his account during login time. Is there any method to do so? I haven't found any configuration like prompt=select_account+consent.
Now, with these codes, after a user logout and then try to login again, it will automatically sign in with the selected account, there is no window showing up for user to select an account.
pubspec.yaml
firebase_auth: ^0.8.1+4
google_sign_in: ^3.2.4
Login part
GoogleSignInAccount googleUser = await _googleSignIn.signIn();
GoogleSignInAuthentication googleAuth = await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
user = await _auth.signInWithCredential(credential);
Logout part
await FirebaseAuth.instance.signOut();
GoogleSignIn _googleSignIn = GoogleSignIn();
await _googleSignIn.signOut();
Use GoogleSignInAccount.disconnect() before signing out to revoke the previous authentication:
await _googleSignIn.disconnect();
await FirebaseAuth.instance.signOut();
Harold's answer used to work for me, but recently the GoogleSignIn().currentUser appears null for some devices I tested, and then the disconnect function won't work. So, what solved that problem was ensuring it is signed in to Google.
final googleCurrentUser =
GoogleSignIn().currentUser ?? await GoogleSignIn().signIn();
if (googleCurrentUser != null)
await GoogleSignIn().disconnect().catchError((e, stack) {
FirebaseCrashlytics.instance.recordError(e, stack);
});
await _auth.signOut();
A simple way to go about it :-
In your sign out method just use
_auth.signOut();
Now inside Google Sign In package, inside google_sign_in.dart
Future<GoogleSignInAccount> signIn() {
final Future<GoogleSignInAccount> result =
_addMethodCall(GoogleSignInPlatform.instance.signIn, canSkipCall: false);
bool isCanceled(dynamic error) =>
error is PlatformException && error.code == kSignInCanceledError;
return result.catchError((dynamic _) => null, test: isCanceled);
}
Find the above method & set the canSkipCall parameter as false
final Future<GoogleSignInAccount> result =
_addMethodCall(GoogleSignInPlatform.instance.signIn, canSkipCall: false);
This will enable choosing a user every time you try to sign in