首页 > 解决方案 > SwiftUI - 重新验证用户凭据 (FireStore)

问题描述

好的,所以我在网上查看,甚至阅读了谷歌提供的文件,但是,我似乎无法弄清楚我哪里出错了。

我想更改一个帐户的密码。这是我现在的代码:

var credential: AuthCredential!

let user = Auth.auth().currentUser

VStack {
                Text("Enter new password").font(.caption).foregroundColor(Color(SYSTEM_FONT_COLOUR))
                       PasswordTextField(password: $changeLoginDetailsViewModel.newPassword)
                Spacer()
                SignInButton(action: {
                    self.user?.reauthenticate(with: self.credential) { (result, error) in
                        if let error = error {
                            print(error.localizedDescription)
                        }
                    }
                    self.changeLoginDetailsViewModel.changePassword()
                }, label: "Update Password")
            }

每次我点击"Update password"按钮时,应用程序都会崩溃。那是因为credential是零。所以我做了我的研究并找到了方法EmailAuthProvider()。所以我添加了这行代码:

let email = EmailAuthProvider.credential(withEmail: (Auth.auth().currentUser?.email!)!, password: "thisIsNotTheRealPassword")

出于道德原因和最佳实践,我不在我的数据库中存储用户密码。那么我什至如何获取/访问当前用户的密码呢?不保存?

我想我可以让用户输入他们当前的密码,这将允许重新身份验证,但是有没有一种方法可以在视图加载时做到这一点?所以他们不需要按两次更新密码按钮?并获得更好的 UI 体验。他们的用户已经登录,除非他们退出,否则不需要登录。

标签: swiftfirebasefirebase-authentication

解决方案


好的,所以为了确保我始终对用户进行身份验证,我要求用户在有机会/查看更新他们的凭据之前输入他们的登录凭据。

我的解决方案:

Settings > Re-authentication view (explained to the user it's for security purposes) > Change password and e-mail view

推荐阅读