reopening of google signin popup

25 Views Asked by At

This Flutter code defines functions for Google and Facebook sign-ins, including error handling. The _login function handles email/password authentication and displays relevant dialogs. The _onWillPop function checks for a Google sign-in popup, dismissing it if in progress, and prevents accidental app exits. _exitDialogShown prevents multiple confirmation dialogs when trying to exit the app.

 Future<void> googleSignIn() async {
    try {
      if (_isGoogleSignInPopupOpen) {
        return; // Don't proceed if the popup is already open
      }
      _isGoogleSignInPopupOpen = true;

      FirebaseAuth auth = FirebaseAuth.instance;
      final GoogleSignIn googleSignIn0 = GoogleSignIn(
        scopes: [
          'https://www.googleapis.com/auth/drive',
        ],
      );
      await googleSignIn0.signIn();
      final GoogleSignInAccount? googleUser = await googleSignIn0.signIn();

      if (googleUser != null) {
        final GoogleSignInAuthentication googleAuth = await googleUser.authentication;

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

        final UserCredential userCredential = await auth.signInWithCredential(credential);
        print("User signed in with Google: ${userCredential.user?.displayName}");
      } else {
        print("Google Sign-In Cancelled");
      }
    } catch (error) {
      print("Google Sign-In Error: $error");
    } finally {
      _isGoogleSignInPopupOpen = false;
    }
  }


  Future<UserCredential?> signInWithFacebook() async {
    try {
      final LoginResult result = await FacebookAuth.instance.login();

      if (result.status == LoginStatus.success) {
        final AccessToken accessToken = result.accessToken!;
        final OAuthCredential facebookAuthCredential =
        FacebookAuthProvider.credential(accessToken.token);

        return await FirebaseAuth.instance.signInWithCredential(
            facebookAuthCredential);
      } else {
        print("Facebook Sign-In Cancelled");
        return null; // Return null in case of cancellation
      }
    } catch (error) {
      print("Facebook Sign-In Error: $error");
      return null; // Return null in case of an error
    }
  }


  String _email = '';
  String _password = '';

  // Function to handle the login button press
  void _login() async {
    if (_email.isNotEmpty && _password.isNotEmpty) {
      try {
        // Use _authService to handle login logic
        UserCredential? userCredential = await _authService.signInWithEmailAndPassword(_email, _password);

        if (userCredential != null && userCredential.user != null) {
          Navigator.pushNamed(context, '/home2');
        } else {
          // Authentication failed, show an error message.
          print('Authentication failed');
          showDialog(
            context: context,
            builder: (context) {
              return AlertDialog(
                title: const Text('Login Failed'),
                content: const Text('Invalid email or password. Please try again.'),
                actions: [
                  TextButton(
                    onPressed: () {
                      Navigator.pop(context);
                    },
                    child: const Text('OK'),
                  ),
                ],
              );
            },
          );
        }
      } catch (error) {
        // Handle other errors, e.g., network issues.
        print('Error during login: $error');
        showDialog(
          context: context,
          builder: (context) {
            return AlertDialog(
              title: const Text('Login Failed'),
              content: const Text('An error occurred during login. Please try again.'),
              actions: [
                TextButton(
                  onPressed: () {
                    Navigator.pop(context);
                  },
                  child: const Text('OK'),
                ),
              ],
            );
          },
        );
      }
    } else {
      print('Please enter valid email and password');
    }
  }

  bool _exitDialogShown = false; // Add this flag at the beginning of your _MyLoginState class.

  Future<bool> _onWillPop() async {
    // Check if the Google sign-in popup is open
    bool isGoogleSignInInProgress = false;

    try {
      final GoogleSignIn googleSignIn = GoogleSignIn();
      final GoogleSignInAccount? account = await googleSignIn.signInSilently();
      isGoogleSignInInProgress = account != null;
    } catch (error) {
      print("Error checking Google sign-in status: $error");
    }

    if (isGoogleSignInInProgress) {
      // Dismiss the Google sign-in popup
      final GoogleSignIn googleSignIn = GoogleSignIn();
      await googleSignIn.signOut();
      return false; // Prevent app from exiting
    }

    if (_exitDialogShown) {
      // If the exit confirmation dialog was already shown, allow navigation to happen.
      return true;
    }

functions for Google authentication and displays relevant dialogs. The _onWillPop function checks for a Google sign-in popup, dismissing it if in progress, and prevents accidental app exits. _exitDialogShown prevents multiple confirmation dialogs when trying to exit the app.

0

There are 0 best solutions below