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?
You can implement this in two steps:
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.