Search code examples
emaildartflutterverificationaccount

Flutter: way to validate one sort of google account?


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.


Solution

  • 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.