Flutter:如何使用电子邮件验证防火墙认证

qvsjd97n  于 2022-12-19  发布在  Flutter
关注(0)|答案(2)|浏览(192)

我刚刚完成了我的第一个应用程序的Firebase Auth,但我想在用户注册时添加电子邮件验证,请问我如何才能做到这一点。

class AuthServices {
      final auth.FirebaseAuth _firebaseAuth = auth.FirebaseAuth.instance;
    
  Login? _userFromFirebase(auth.User? user) {
    if (user == null) {
      return null;
    }
    return Login(user.uid, user.email);
  }

  Stream<Login?>? get user {
    return _firebaseAuth.authStateChanges().map(_userFromFirebase);
  }

  Future<Login?> signUp(String email, String password, String name) async {
    final credential = await _firebaseAuth.createUserWithEmailAndPassword(
        email: email, password: password);
    UserService().createUser(name);
    return _userFromFirebase(credential.user);
  }

  Future<Login?> signIn(String email, String password) async {
    final credential = await _firebaseAuth.signInWithEmailAndPassword(
        email: email, password: password);
    return _userFromFirebase(credential.user);
  }

  Future<void> signOut() async {
    return await _firebaseAuth.signOut();
  }
}
xesrikrc

xesrikrc1#

这将做你的工作:-

final firebaseUser = await FirebaseAuth.instance
.signInWithEmailAndPassword(email: email, password: password);
 if (firebaseUser.isEmailVerified){ 
       //Verified 
    } 
   else {
   firebaseUser.sendEmailVerification();
      }
cuxqih21

cuxqih212#

Future<Login?> signUp(String email, String password, String name) async {
    final credential = await _firebaseAuth.createUserWithEmailAndPassword(
        email: email, password: password);

    await credential.user?.sendEmailVerification();  // Add this line

    UserService().createUser(name);
    return _userFromFirebase(credential.user);
  }

相关问题