简体   繁体   中英

Flutter Firebase change password of user with 2FA enabled

When i sign in with 2FA (email provider, phone provider) i just do signInWithEmailAndPassword , then i catch FirebaseAuthMultiFactorException , get MultiFactorResolver to resolve second factor and sign in successfully. Similar for sign up. The problem arises when i try to modify user data, like to change password.

I tried the same flow as we do for sign in/ sign up flow, but it doesn't work, it returns FirebaseException instead of FirebaseAuthMultifactorException :

Future<void> changePassword({required String email, required String password, required String newPassword}) async{
    final user = _firebaseAuth.currentUser!;
    try{
      final cred = EmailAuthProvider.credential(email: email, password: password);
      await user.reauthenticateWithCredential(cred);
    } on FirebaseAuthMultiFactorException catch(e){
      log("i need to get resolver here but this line is never executed");
    } on FirebaseException catch(e){
      log("firebase exception, which doesn`t provide resolver");
      rethrow;
    }
  }

The output is following:

[log] firebase exception, which doesn`t provide resolver
E/flutter (18929): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: [firebase_auth/second-factor-required] Please complete a second factor challenge to finish signing into this account.
E/flutter (18929): #0      parseMultiFactorError (package:firebase_auth_platform_interface/src/method_channel/utils/exception.dart:106:5)
E/flutter (18929): #1      platformExceptionToFirebaseAuthException (package:firebase_auth_platform_interface/src/method_channel/utils/exception.dart:49:14)
E/flutter (18929): #2      convertPlatformException (package:firebase_auth_platform_interface/src/method_channel/utils/exception.dart:23:5)
E/flutter (18929): #3      MethodChannelUser.reauthenticateWithCredential (package:firebase_auth_platform_interface/src/method_channel/method_channel_user.dart:181:7)
E/flutter (18929): <asynchronous suspension>
E/flutter (18929): #4      User.reauthenticateWithCredential (package:firebase_auth/src/user.dart:452:7)
E/flutter (18929): <asynchronous suspension>
E/flutter (18929): #5      AuthRepository.changePassword (package:vendor/repositories/auth/auth_repository.dart:43:7)
E/flutter (18929): <asynchronous suspension>
E/flutter (18929): #6      new AuthBloc.<anonymous closure> (package:vendor/blocs/auth_bloc/auth_bloc.dart:50:7)
E/flutter (18929): <asynchronous suspension>
E/flutter (18929): #7      Bloc.on.<anonymous closure>.handleEvent (package:bloc/src/bloc.dart:226:13)
E/flutter (18929): <asynchronous suspension>

That was flutter firebase-sdk issue that was fixed in latest versions of firebase_auth package

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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