我在处理 Flutter 的 Firebase 身份验证错误时遇到问题?

时间:2021-07-26 13:47:05

标签: firebase flutter

未处理的异常:[firebase_auth/user-not-found] 没有与此标识符对应的用户记录。该用户可能已被删除。 E/flutter (10288): #0 MethodChannelFirebaseAuth.signInWithEmailAndPassword (package:firebase_auth_platform_interface/src/method_channel/method_channel_firebase_auth.dart

// 这个类在模型中

class HttpException implements Exception{
  final String message;

  HttpException(this.message);

  @override
  String toString() {
    return message;
    // return super.toString();
  }

}

// 此功能在认证界面

Future<void> _submit() async {
    if (!_formKey.currentState.validate()) {
      // Invalid!
      return;
    }
    if (_userImageFile == null && !_isLoading && _authMode != AuthMode.Login) {
      ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('Please pick an image'),));
      return;
    }
    _formKey.currentState.save();
    setState(() {
      _isLoading = true;
    });
    try {
      if (_authMode == AuthMode.Login) {
        AuthService()
            .logInUser(_authData['email'], _authData['password'])
            .then((value) {
          if (value != null) {
            Navigator.of(context).pushReplacementNamed(Home.routeName);
          } else {
            ScaffoldMessenger.of(context).showSnackBar(
                SnackBar(content: Text('error'),));
          }
        });
      } else {
        AuthService().createUser(_authData['email'], _authData['password'],
            _authData['name'], _authData['status'], _authData['companyName'],
            _userImageFile).then((value) {
          if (value != null) {
            Navigator.of(context).pushReplacementNamed(Home.routeName);
          } else {
            ScaffoldMessenger.of(context).showSnackBar(
                SnackBar(content: Text('error'),));
          }
        });
      }
    }  on HttpException catch (error) {
      var message = 'An error occurred, please check your credentials!';

      if (error.message != null) {
        message = error.message;
      }

      if (Platform.isAndroid) {
        var errorMessage = 'Authentication failed';
        if (error.toString().contains('EMAIL_EXISTS')) {
          errorMessage = 'This email address is already in use.';
        } else if (error.toString().contains('INVALID_EMAIL')) {
          errorMessage = 'This is not a valid email address.';
        } else if (error.toString().contains('WEAK_PASSWORD')) {
          errorMessage = 'This password it too weak.';
        } else if (error.toString().contains('EMAIL_NOT_FOUND')) {
          errorMessage = 'Could not find a user with that email.';
        } else if (error.toString().contains('wrong-password')) {
          errorMessage = 'This is not a valid password.';
        }
        _showErrorDialog(errorMessage);
      }
    }catch(error){
      print(error);
    }
    setState(() {
      _isLoading = true;
    });
  }

// 这个类在 Auth Services 中

class AuthService {
  final FirebaseAuth _auth = FirebaseAuth.instance;

  Future<User> createUser(String email, String password, String name,
      String status, String companyName,File image) async {

    final authResult = await _auth.createUserWithEmailAndPassword(
        email: email, password: password);

    final ref = await FirebaseStorage.instance.ref().child('user_image').child(
        authResult.user.uid + '.jpg');

    await ref.putFile(image);

    final url = await ref.getDownloadURL();

    await FirebaseFirestore.instance
        .collection('users')
        .doc(authResult.user.uid)
        .set({
      'username': name,
      'email': email,
      'status': status,
      'companyName': companyName,
      'id': authResult.user.uid,
      'image_url':url,
    });
    return authResult.user;
  }

  Future<User> logInUser(String email, String password) async {
      final authResult = await _auth.signInWithEmailAndPassword(
          email: email, password: password);
      return authResult.user;
  }

}``` 

0 个答案:

没有答案