How to add email verification in this flutter? I need to verify users before signing them in
342
You can implement this in two steps:
- Send the verification mail after creating the user
- Check if the user is verified and decide what UI to show when the user tries to login in
Sending verification mail:
void registerNewUser(BuildContext context) async {
... //Your existing code for creating the new user
if (firebaseUser != null) {
Map userDataMap = {
"name": nameTextEditingController.text.trim(),
"email": emailTextEditingController.text.trim(),
"phone": phoneTextEditingController.text.trim(),
};
usersRef.child(firebaseUser.uid).set(userDataMap);
await firebaseUser.sendEmailVerification();
await handleUserEmailVerification(context, user: firebaseUser);
}
... //Rest of your code
}
Checking if the user is verified
Future<void> handleUserEmailVerification(BuildContext context, {@required User user}) async {
if (user.isEmailVerified) {
// Navigate the user into the app
} else {
// Tell the user to go verify their mail
}
}
You can also use this handleUserEmailVerification
method for sign-in.
Signing a user in
Future<void> signInUser(BuildContext context) async {
final FirebaseAuth firebaseAuth = FirebaseAuth.instance;
User firebaseUser = await firebaseAuth
.signInWithEmailAndPassword(
email: loginEmailController.text,
password: loginPasswordController.text,
);
await handleUserEmailVerification(context, user: firebaseUser);
}
Author by
Sandeep Kshetri
Updated on November 27, 2022Comments
-
Sandeep Kshetri over 1 year
This code must provide me email verification for the user when they click sign in firstly they have to verify their mail afterwards they can sign in
onPressed: () async{ if (nameTextEditingController.text.length < 4) { displayToastMessage( "Username Must be atleast 4 characters", context); } else if (!emailTextEditingController.text .contains("@")) { displayToastMessage("Email is not Valid ", context); } else if ((phoneTextEditingController.text.length <10 )) { displayToastMessage( "Invalid Phone number ", context); } else if (passwordTextEditingController.text.length < 7) { displayToastMessage( "Password must be 6 characters long", context); } else { registerNewUser(context); } }), ], ), ), FlatButton( onPressed: () { Navigator.pushNamedAndRemoveUntil( context, LoginScreen.idscreen, (route) => false); }, child: Text("Already have an Account ? Login Here",style: TextStyle(fontSize: 17,color: Colors.white),)), ], ), ), ), ); final FirebaseAuth _firebaseAuth = FirebaseAuth.instance; void registerNewUser(BuildContext context) async { showDialog( context: context, barrierDismissible: false, builder: (BuildContext context) { return ProgressDialog(message: "Signing In , please wait....",); }); final User firebaseUser = (await _firebaseAuth .createUserWithEmailAndPassword( email: emailTextEditingController.text, password: passwordTextEditingController.text) .catchError((errMsg){ Navigator.pop(context); displayToastMessage("Error:"+errMsg.toString(), context); }) ) .user; if (firebaseUser != null) //user has been created { Map userDataMap = { "name": nameTextEditingController.text.trim(), "email": emailTextEditingController.text.trim(), "phone": phoneTextEditingController.text.trim(), }; usersRef.child(firebaseUser.uid).set(userDataMap); displayToastMessage("Welcome to Yatra. Book ride as you like", context); Navigator.pushNamedAndRemoveUntil( context, MainScreen.idscreen, (route) => false); } else { Navigator.pop(context); //if error display messages displayToastMessage("UserAccount hasn't been Created", context); } } } displayToastMessage(String message, BuildContext context) { Fluttertoast.showToast(msg: message); }
I came across the sendEmailVerification(); method provided by firebase_auth package, but need some advice on setting it up. Does anyone have a working code example to follow?
-
Sandeep Kshetri almost 3 yearsgetting an error Undefined name 'context'. for await handleUserEmailVerification(context, user: firebaseUser);
-
Victor Eronmosele almost 3 years@SandeepKshetri, I've updated the answer by passing the
BuildContext
to thesignInUser
method.