Flutter: way to validate one sort of google account?

867

You can use google sign in integration like this.

  GoogleSignInAccount _currentUser;

then in init state:-

@override
  void initState() {
    super.initState();

  _googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount account) {
   setState(() {
     _currentUser = account;
     print("User Name ${_currentUser.displayName}");
     print("User Email ${_currentUser.email}");
    });

   if (_currentUser != null) {
     var socialData = SocialData(
        _currentUser.displayName, "", _currentUser.email, LoginType.GOOGLE);
     _startHomeScreen(socialData);
   } else {
    _showError('Error, Please try again later');
   }
 });

}

on successful login, this will execute:-

 _startHomeScreen(SocialData data) {
    Navigator.push(context, MaterialPageRoute(builder: (context) {
      return Home(socialData: data);
    }));
  }


     Future<void> _handleSignIn() async {
    try {
    await _googleSignIn.signIn();
     } catch (error) {
    print(error);
   }
  }

Call _handleSignIn() on click of google sign in button.

Share:
867
Igor D
Author by

Igor D

Updated on December 11, 2022

Comments

  • Igor D
    Igor D over 1 year

    I'm building an app in flutter (latest build) for students on the university. every student has its own email and password (ex. [email protected]), which is integrated in google, so everyone is able to see notifications in Gmail.

    This does also mean; if you want to log in with google, your studentEmail is an option to do so. I want to implement a google log-in feature where only student of the university (with their email [email protected]) are able to login.

    My question is: is there a way to filter on the google login email? I thought about using normal email login and use RegEx to validate, but this means student should firstly signup. I would like to skip the whole sign-up and let the students use their already owned student email to signin.

    it should look something like this (if it is even possible) VV

    Widget googleLogin () {
    googleLogin button()
    if(googlelogin.email == [email protected])
        log user in;
    } else {
        return error message('invalid email')
    }
    

    I want this to be able to only register and login user with student email.

  • Igor D
    Igor D almost 5 years
    thanks! I will try and see if this implementation works for me. I appreciate you helping me :)
  • Igor D
    Igor D almost 5 years
    is this using the google_sign_in dart package? and how do I validate that the user is a university student, as I do not see any validations? still thanks for helping!
  • Ammar Hussein
    Ammar Hussein almost 5 years
    You're welcome, don't forget to mark it as the right answer if it was helpful :)
  • Igor D
    Igor D almost 5 years
    hello sir. this was a good recommendation, but there is currently no way to create a google login with flutter on firebase. the firebase_auth has removed the signinWith google option and i am not sure how to create a google login now anymore :/
  • Ammar Hussein
    Ammar Hussein almost 5 years
    github.com/ammaratef45/Attendance/blob/master/attendance/lib‌​/… this is how I handle it in my project, remember to use the same versions of packages in my pubspec file
  • Igor D
    Igor D almost 5 years
    thanks! i found another git with the same implementation as yours. i still have some problems with the setstate, as this is giving me errors (setstate called after dispose) i changed from a login form to google, and there still might be some dirty code. :/ would you mind taking a look?