繁体   English   中英

如何验证 email 和密码 flutter firebase

[英]how to verify email and password flutter firebase

我是 flutter 的新手,我想在我的应用程序上实现登录屏幕。 为此,我使用 flutter 和 firebase。 问题是,当 email 和密码字段中的任何内容时,应用程序访问主页。 所以我需要在获得访问权限之前验证 email。

这是代码

谢谢大家帮助我,

               onPressed: () {
                if (_formKey.currentState.validate()) {
                  Future<String> check = logIn();
                  if (check != null) {
                    Navigator.pushReplacement(
                        context,
                        MaterialPageRoute(
                            builder: (BuildContext context) =>
                                HomePage()));
                  }
                }
              

Firebase 提供了一个 function 用于检查我在下面的代码中使用的用户是否经过验证。

Future<bool> login(
  String email, String password) async {
final user = (await FirebaseAuth.instance
        .signInWithEmailAndPassword(email: email, password: password))
    .user;
if (user.isEmailVerified) {
  return true;
}
return false;
}

但是,要使用此 function,您必须首先在您的用户创建帐户时调用的 function 中的某处向您的用户发送 email 验证。 下面的 function 将创建用户并发送验证 email ,然后在完成后返回FirebaseUser

Future<FirebaseUser> register() async {
await _auth
    .createUserWithEmailAndPassword(email: email.trim(), password: password)
    .then(
  (result) async {
    //send verifcation email
    result.user.sendEmailVerification();
    return result.user;
  },
);
return null;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM