繁体   English   中英

Firebase 尝试登录时显示无效电子邮件/错误的消息

[英]Firebase Message to display invalid email/error when trying to sign in

我对 flutter 很陌生,我已经用登录按钮连接了 Firebase function。 它工作得很好,我唯一想做的就是在 email 和密码未找到、未注册等情况下添加错误消息,例如用户帐户的 firebase 内没有已知 ID。 它将显示“找不到用户”、无效的 email 或密码等。

Container(
                padding: const EdgeInsets.symmetric(horizontal: 40),
                child: Column(
                  children: [
                    Padding(
                      padding: const EdgeInsets.symmetric(vertical: 15.0),
                      child: Container(
                        decoration: BoxDecoration(
                            color: Colors.grey[500].withOpacity(0.5),
                            borderRadius: BorderRadius.circular(16)),
                        child: TextFormField(
                          onChanged: (value) {
                            setState(() {
                              _email = value.trim();
                            });
                          },
                          decoration: InputDecoration(
                            contentPadding:
                                const EdgeInsets.symmetric(vertical: 20.0),
                            border: InputBorder.none,
                            hintText: 'Email',
                            hintStyle: TextStyle(
                                fontSize: 20.0, color: Colors.white),
                            prefixIcon: Padding(
                              padding: const EdgeInsets.symmetric(
                                  horizontal: 20.0),
                              child: Icon(
                                FontAwesomeIcons.solidEnvelope,
                                color: Colors.white,
                                size: 20.0,
                              ),
                            ),
                          ),
                          style: TextStyle(
                              fontSize: 20.0, color: Colors.white),
                          keyboardType: TextInputType.emailAddress,
                          textInputAction: TextInputAction.next,
                        ),
                      ),
                    ),
                    Container(
                      decoration: BoxDecoration(
                          color: Colors.grey[500].withOpacity(0.5),
                          borderRadius: BorderRadius.circular(16)),
                      child: TextFormField(
                        onChanged: (value) {
                          setState(() {
                            _password = value.trim();
                          });
                        },
                        decoration: InputDecoration(
                            contentPadding:
                                const EdgeInsets.symmetric(vertical: 20.0),
                            border: InputBorder.none,
                            hintText: 'Password',
                            hintStyle: TextStyle(
                                fontSize: 20.0, color: Colors.white),
                            prefixIcon: Padding(
                              padding: const EdgeInsets.symmetric(
                                  horizontal: 20.0),
                              child: Icon(
                                FontAwesomeIcons.lock,
                                color: Colors.white,
                                size: 20.0,
                              ),
                            ),
                            suffixIcon: Icon(
                              FontAwesomeIcons.eye,
                              color: Colors.white,
                              size: 20.0,
                            )),
                        obscureText: true,
                        style:
                            TextStyle(fontSize: 20.0, color: Colors.white),
                        keyboardType: TextInputType.name,
                        textInputAction: TextInputAction.done,
                      ),
                    ),
                    SizedBox(
                      height: 20.0,
                    ),
                    RoundedButton(
                        text: "Login",
                        color: Colors.grey[600],
                        press: () async {
                          await auth.signInWithEmailAndPassword(
                              email: _email, password: _password);
                          Navigator.of(context).pushReplacement(
                              MaterialPageRoute(
                                  builder: (context) => WelcomeScreen()));
                        }),

任何帮助表示赞赏。 谢谢你。 我对堆栈溢出也很陌生。 抱歉,如果我的问题格式错误

您应该始终使用 try/catch 语句包围您的登录代码:

try {
    await auth.signInWithEmailAndPassword(
       email: _email, password: _password);
} on FirebaseAuthException catch (e) {
    //here you get the error messages and do stuff accordingly
    if(e.code == 'wrong-password') {
        //handle wrong password like in an alertdialog
    }
}

发生错误时将返回不同的代码:

  • '未找到用户'
  • '不合规电邮'
  • '用户禁用'
  • '不允许操作'

等等。 您可以在此站点上查看完整的错误代码列表:

Firebase 错误

暂无
暂无

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

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