首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更改密码前用当前密码验证旧密码

更改密码前用当前密码验证旧密码
EN

Stack Overflow用户
提问于 2022-01-19 18:45:58
回答 1查看 418关注 0票数 1

我想更改firebase的当前密码。但是,如何使用旧密码使用颤振验证当前密码?

我的change_password.dart表单文件

代码语言:javascript
复制
Form(
      key: _formKey,
      child: Column(
        children: [
    
          TextFormField(
            controller: _oldPasswordController,
            obscureText: hidePassword,
            decoration: InputDecoration(
              labelText: AppString.oldPassword,
            ),
            validator: (value) {
              if (value!.isEmpty) {
                return AppString.enterPassword;
              } else if (value.length <= 5) {
                return AppString.passwordValidation;
              }
              return null;
            },
          ),
        
          TextFormField(
            controller: _changePasswordController,
            obscureText: hidePassword,
            decoration: InputDecoration(
              labelText: AppString.newPassword,    
            ),
            validator: (value) {
              if (value!.isEmpty) {
                return AppString.enterPassword;
              } else if (value.length <= 5) {
                return AppString.passwordValidation;
              }
              return null;
            },
          ),
          
          Row(
            children: [
                child: ElevatedButton(
                  style: buttonStyle,
                  onPressed: () async {
                    if (_formKey.currentState!.validate()) {
                      _formKey.currentState!.save();
                      changePassword(
                        _changePasswordController!.text,
                      );
                    }
                  },
                  child: Text(
                          AppString.changePassword,
                        ),
                ),

更改密码函数,其中从小部件:调用函数

代码语言:javascript
复制
void changePassword(String password) async {
    try {
      await currentUser!.updatePassword(password);
      AuthProvider.logOut().whenComplete(() {
        snackBarWidgets(context, AppString.pswdChangedSuccessful);
      });
    } catch (e) {
      rethrow;
    }
  }

图像

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-20 06:52:56

在调用updatePassword之前添加以下行

代码语言:javascript
复制
final user = FirebaseAuth.instance.currentUser;
// you should check here that email is not empty
final credential = EmailAuthProvider.credential(
    email: user.email!,
    password: <password>);
try {
  await user.reauthenticateWithCredential(credential);
  // proceed with password change
} on FirebaseAuthException catch (e) {
  // handle if reauthenticatation was not successful
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70776144

复制
相关文章

相似问题

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