signInWithGoogle method

dynamic signInWithGoogle(
  1. BuildContext context
)

Implementation

signInWithGoogle(BuildContext context) async {
  final FirebaseAuth firebaseAuth = FirebaseAuth.instance;
  final GoogleSignIn googleSignIn = GoogleSignIn();

  final GoogleSignInAccount? googleSignInAccount =
      await googleSignIn.signIn();

  final GoogleSignInAuthentication googleSignInAuthentication =
      await googleSignInAccount!.authentication;

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

  UserCredential result = await firebaseAuth.signInWithCredential(credential);

  User userDetails = result.user!;

  SharedPreferenceHelper().saveUserEmail(userDetails.email!);
  SharedPreferenceHelper().saveUserId(userDetails.uid);
  SharedPreferenceHelper()
      //.saveUserName(userDetails.email!.replaceAll("@gmail.com", ""));
      .saveUserName(userDetails.displayName!);
  SharedPreferenceHelper().saveDisplayName(userDetails.displayName!);
  SharedPreferenceHelper().saveUserProfileUrl(userDetails.photoURL!);

  Map<String, dynamic> userInfoMap = {
    "email": userDetails.email,
    "username": userDetails.email!.replaceAll("@gmail.com", ""),
    "name": userDetails.displayName,
    "imgUrl": userDetails.photoURL,
    "userIdKey": userDetails.uid,
  };

  DatabaseMethods()
      .addUserInfoToDB(userDetails.uid, userInfoMap)
      .then((value) {
    DatabaseMethods().addUserJoinGroup(userDetails.uid);
    Navigator.pushReplacement(
        context, MaterialPageRoute(builder: (context) => const GroupsPage()));
  });
}