首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Firebase身份验证!即使我输入了一个未注册的用户,我也可以转到主页

Firebase身份验证!即使我输入了一个未注册的用户,我也可以转到主页
EN

Stack Overflow用户
提问于 2021-04-19 19:28:17
回答 1查看 25关注 0票数 0

我已经成功地将firebase添加到我的flutter2.0项目中。我还启用了身份验证。我还可以注册一个新用户。但是,在身份验证后转到主页的逻辑不起作用。即使我输入了错误的用户,我也可以登录。我想如果我输入正确的用户,然后我应该导航到主页,但如果用户没有注册它应该不能导航到下一页。ie..the身份验证的基本用法。但它不会发生在这里。即使我输入了错误的用户,我也能够导航到下一个位置。

代码语言:javascript
复制
Future signInWithEmailAndPassword(String email, String password) async {
    try {
  UserCredential userCredential = await FirebaseAuth.instance.signInWithEmailAndPassword(
    email:email,
    password:password,
  );
  
} on FirebaseAuthException catch (e) {
  if (e.code == 'user-not-found') {
    print('No user found for that email.');
  } else if (e.code == 'wrong-password') {
    print('Wrong password provided for that user.');
  }
}
  }

  Future newAccount(String email, String password) async {
    try {
      UserCredential userCredential = await FirebaseAuth.instance
          .createUserWithEmailAndPassword(email: email, password: password);
    } on FirebaseAuthException catch (e) {
      if (e.code == 'weak-password') {
        print('The password provided is too weak.');
      } else if (e.code == 'email-already-in-use') {
        print('The account already exists for that email.');
      }
    } catch (e) {
      print(e.toString());
    }
  }
}

//this is how I can this function and try to navigate to next page.
void logMeIn() {
    if (formKey.currentState!.validate()) {
      authMethods
          .signInWithEmailAndPassword(usernameTextEditingContoller.text,
              passwordTextEditingContoller.text)
          .then((value) {
            print('value is: ');
        print(value);
        Navigator.of(context)
            .pushReplacement(MaterialPageRoute(builder: (context) {
          return Home();
        }));
      });
    }
  }**strong text**
EN

回答 1

Stack Overflow用户

发布于 2021-04-19 19:35:43

signInWithEmailAndPassword返回一个UserCredential对象。

在您的逻辑中,您正在使用.then((value) {...etc})

valuesignInWithEmailAndPassword的结果。

尝试将您的逻辑更改为:

代码语言:javascript
复制
authMethods
          .signInWithEmailAndPassword(usernameTextEditingContoller.text,
              passwordTextEditingContoller.text)
          .then((value) {
            print('value is: ');
        print(value);
        if(value.user ==null) return "Error in authentication"; // this will prevent your function form going further down the navigation if the usercredential doesn't have a valid user.
        Navigator.of(context)
            .pushReplacement(MaterialPageRoute(builder: (context) {
          return Home();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67161130

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档