error when i click on google sign in flutter - flutter

When I click on the "google sign in" button and then instead of continuing I click on "cancel", this error comes out: "PlatformException (PlatformException (sign_in_canceled, com.google.GIDSignIn, The user canceled the sign-in flow., Null))."
The code is this:
Future<UserCredential?> signInWithGoogle() async {
try {
// Trigger the authentication flow
final GoogleSignInAccount? utenteGoogle = await GoogleSignIn().signIn();
// Obtain the auth details from the request
final GoogleSignInAuthentication? datiUtenteGoogle =
await utenteGoogle?.authentication;
// Create a new credential
final credenzialiGoogle = GoogleAuthProvider.credential(
accessToken: datiUtenteGoogle?.accessToken,
idToken: datiUtenteGoogle?.idToken,
);
// Once signed in, return the UserCredential
return await FirebaseAuth.instance.signInWithCredential(credenzialiGoogle);
} catch (e) {
print(e);
return null;
}
}
I tried to catch the error but nothing

Related

An error occurs when Google Sign in canceled

When user pushes a button, signInWithGoogle() will be called.
Future<void> signInWithGoogle() async {
final GoogleSignInAccount? googleUser;
final GoogleSignInAuthentication googleAuth;
googleUser = await GoogleSignIn().signIn().catchError((e) {
context.read<LoginModel>().load(false); //Have the user login again
});
if (googleUser == null) {
return;
}
googleAuth = await googleUser.authentication;
final OAuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
await FirebaseAuth.instance.signInWithCredential(credential);
mainpage.accountCreated();
context.read<mainpage.UserInfo>().setUser();
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text("Success"),
));
context.read<mainpage.UserInfo>().getUserInfo();
}
If signin succeeded, no error has occurs.
However if the process canceled, the error below has occured.
throw PlatformException(code: errorCode, message: errorMessage as String?, details: errorDetails, stacktrace: errorStacktrace);
What should I do?

Error getting thrown when trying to sign in with Google Auth to Firestore

I am trying to create a google sign in option for my app. I appear to be getting back a valid Token Id from google but my app is crashing in the ios Emulator and the following error is being shown in console.
flutter: [firebase_auth/invalid-credential] Unable to parse Google id_token: ya29.A0ARrdaM_uqbYHorJh1kJXTXac7MEm2TjD.......
When I cancel the login an error is successfully being thrown from my code. Can anyone help me out?
flutter: [firebase_auth/sign_in_canceled] The user canceled the sign-in flow.
#override
Future<User?> signInWithGoogle() async {
final googleSignIn = GoogleSignIn();
final googleUser = await googleSignIn.signIn();
if (googleUser != null) {
final googleAuth = await googleUser.authentication;
if (googleAuth.idToken != null) {
final UserCredential = await _firebaseAuth
.signInWithCredential(GoogleAuthProvider.credential(
idToken: googleAuth.accessToken,
));
return UserCredential.user;
} else {
throw FirebaseAuthException(
code: 'ERROR_MISSING_GOOGLE_ID_TOKEN',
message: 'Missing Google ID Token',
);
}
} else {
throw FirebaseAuthException(
code: 'sign_in_canceled',
message: 'The user canceled the sign-in flow.',
);
}
}
I figured it out. This line needed to be changed to pass the idToken...not the accessToken.
idToken: googleAuth.idToken,

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

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,
);
}

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.

Google Authentication and Facebook Authentication Existing User

Hello Everyone someone please offer me some help if you can. I'm not sure if this is an issue on Firebase's side or if I failed on my code or configuring the Firebase authentication correctly.
So here's the issue.
I wanted to see if a user existed inside the firestore. If it does then link the accounts. I read the documentation and understand that when using the SigninwithCredential() it should throw an error saying something on the lines of "ERROR USER ALREADY EXISTS IN FIRESTORE".
So testing went something like this to see if I'd get an error before trying to handle the error.
Continue with Facebook (OK).
Signed In (OK).
Continue with Google (Expected Error) (Didn't throw Error)
So what happened here instead was the user originally created by Facebook was overridden by Google.
Sign In with Facebook (Expected Error) (OK) Recieved error as expected "An Account already exists but with different sign-in credentials"
Read documentation several times and compared google code with Facebook code and can't seem to find answer.
class MyFirebase {
static bool isSignIn = false;
static auth.FirebaseAuth _auth = auth.FirebaseAuth.instance;
static FacebookLogin facebookLogin = FacebookLogin();
static GoogleSignIn googleSignIn = GoogleSignIn();
static Future<auth.User> loginGoogle() async {
final prefs = await SharedPreferences.getInstance();
final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
if (googleSignInAccount == null) {
print("ERROR HAS OCCURED WITH THE GOOGLE LOGIN");
throw new Exception("ERROR HAS OCCURED WITH THE LOGIN");
} else {
final GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;
var item = googleSignInAccount.photoUrl;
print("PHOTO URL" + item);
prefs.setString("photoURL", item);
final auth.AuthCredential credential = auth.GoogleAuthProvider.credential(
accessToken: googleSignInAuthentication.accessToken);
try {
var authResult = await _auth.signInWithCredential(credential);
return authResult.user;
} catch (e) {
print(e);
throw (e);
}
}
}
static Future<auth.User> loginFacebook() async {
final FacebookLoginResult result = await facebookLogin.logIn(['email']);
final prefs = await SharedPreferences.getInstance();
var a;
switch (result.status) {
case FacebookLoginStatus.cancelledByUser:
print("CANCELLED BY USER DO NOT RETURN");
throw new Exception("CANCELLED BY USER DO NOT RETURN");
break;
case FacebookLoginStatus.error:
print("ERROR OCCURED");
throw new Exception("ERROR oCCURED");
break;
case FacebookLoginStatus.loggedIn:
try {
final FacebookAccessToken accessToken = result.accessToken;
auth.AuthCredential credential =
auth.FacebookAuthProvider.credential(accessToken.token);
print("########## MAKING GRAPH RESPONSE");
final response = await http.get(
'https://graph.facebook.com/v2.12/me?fields=name,first_name,picture.width(800).height(800),last_name,email&access_token=${accessToken.token}');
final profile = jsonDecode(response.body);
String item = profile['picture']['data']['url'];
prefs.setString("photoURL", item);
a = await _auth.signInWithCredential(credential);
return a.user;
} catch (e) {
print(e);
throw e;
}
break;
}
return null;
}
static Future<void> signOut() async {
await facebookLogin.logOut();
await _auth.signOut();
await googleSignIn.signOut();
}
}
**strong text** ```