0

I am making an app in Flutter. Now I have put validator that to validate the email. But I want that if there is an google gmail account or any other valid email (actually has the user) then only the user should create account in app.

For example - Currently if I enter xyz@gmail.com then also the account is created on my app though this email doesn't exists as google account.

So my question is, Is there any way that app should validate first (If the email is valid account on gmail, outlook or any other but should be valid) and then account will create otherwise it should give error that Enter valid email???

I am using Firebase for Authentication.

My Code is below LoginPage.dart

class _LoginPageState extends State<LoginPage> {
final _formKey = GlobalKey<FormState>();
String _email, _password, error = '';
bool _obscureText = true;
final Auth _auth = Auth();

_toggle(){
setState(() {
  _obscureText = !_obscureText;
});
}

_submit() async {
if(_formKey.currentState.validate()){
  _formKey.currentState.save();
  SharedPreferences prefs = await SharedPreferences.getInstance();
  prefs.setString('email', _email);
  dynamic result = await _auth.login(_email, _password);
  if(result == null){
    setState(() => error = 'There is no user record found. Please create account first!!!');
  } else {
    Navigator.pushNamed(context, HomePage.id);
  }
  print(_email);
  print(_password);
}
}

@override
Widget build(BuildContext context) {
return GestureDetector(
  onTap: (){
    FocusScopeNode currentFocus = FocusScope.of(context);

    if (!currentFocus.hasPrimaryFocus) {
      currentFocus.unfocus();
    }
  },
  child: Scaffold(
    body: Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          Text(
            'Login',
            style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
          ),
          Form(
              key: _formKey,
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  Padding(
                    padding: const EdgeInsets.symmetric(
                        horizontal: 30.0, vertical: 10.0),
                    child: Padding(
                        padding: EdgeInsets.only(left: 5, right: 5, top: 5),
                      child: TextFormField(
                        decoration: InputDecoration(
                            labelText: 'Email',
                          border: InputBorder.none,
                          filled: true,
                          fillColor: Colors.white60,
                          focusedBorder: OutlineInputBorder(
                            borderSide: BorderSide(color: Colors.blue,width: 2.0),
                            borderRadius: BorderRadius.circular(10.0)
                          ),
                          /*enabledBorder: UnderlineInputBorder(
                              borderRadius: BorderRadius.circular(10.0)
                          )*/
                        ),
                        validator: (input) => !input.contains('@')
                            ? 'Please enter valid email'
                            : null,
                        onSaved: (input) => _email = input,
                      ),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.symmetric(
                        horizontal: 30.0, vertical: 10.0),
                    child: Stack(
                      alignment: const Alignment(0, 0),
                      children: <Widget>[
                        TextFormField(
                          decoration: InputDecoration(
                              labelText: 'Password',
                            border: InputBorder.none,
                            filled: true,
                            fillColor: Colors.white60,
                            focusedBorder: OutlineInputBorder(
                                borderSide: BorderSide(color: Colors.blue,width: 2.0),
                                borderRadius: BorderRadius.circular(10.0)
                            ),
                          ),
                          validator: (input) => input.length < 6
                              ? 'Must be at least 6 characters'
                              : null,
                          onSaved: (input) => _password = input,
                          obscureText: _obscureText,
                        ),
                        Positioned(
                          right: 15,
                          child: Container(
                            height: 30,
                            child: ElevatedButton(
                              onPressed: (){
                                _toggle();
                              },
                              child: Text(
                                  _obscureText ? 'Show' : 'Hide'
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                  verticalSpaceMedium,
                  Container(
                    width: 200.0,
                    child: TextButton(
                      onPressed: _submit,
                      style: TextButton.styleFrom(
                        primary: Colors.white,
                        backgroundColor: Colors.blue,
                        elevation: 5,
                      ),
                      child: Text(
                        'Login',
                        style: TextStyle(color: Colors.white, fontSize: 16.0),
                      ),
                    ),
                  ),
                  verticalSpaceMedium,
                  Container(
                    width: 200.0,
                    child: TextButton(
                      style: TextButton.styleFrom(
                        primary: Colors.white,
                        backgroundColor: Colors.blue,
                        elevation: 5,
                      ),
                      onPressed: () => Navigator.pushNamed(context, SignupPage.id),
                      child: Text(
                        'Create Account',
                      ),
                    ),
                  ),
                  verticalSpaceMedium,
                  Text(error, style: TextStyle(color: Colors.red, fontSize: 14),)
                ],
              ),
          )
        ],
      ),
    ),
  ),
);

The above code works fine but want to add the validator that I have explained above.

Harry
  • 154
  • 4
  • 15

1 Answers1

1

See https://pub.dev/packages/email_validator. Do not attempt to write a regex to validate an email address. It will most certainly leave out some perfectly valid email forms that you might not have encountered yet. For example, both fred&barney@stonehenge.com (my autoresponder) and *@qz.to (an address a friend used for years, but now uses *@unspecified.example.com) are valid. Email Validator package correctly accepts both of those.

Randal Schwartz
  • 39,428
  • 4
  • 43
  • 70
  • I have used Email Validator but still if I make account from `yyy@gmail.com`, my app creates the account even though this email is not there as gmail. – Harry Apr 04 '21 at 11:36