简体   繁体   English

SwiftUI - 重新验证用户凭据 (FireStore)

[英]SwiftUI - Reauthenticate user credentials (FireStore)

Ok, so I've looked online and even read the documents provided by Google, however, I just cannot seem to figure out where I'm going wrong.好的,所以我在网上看过,甚至阅读了谷歌提供的文件,但是,我似乎无法弄清楚我哪里出错了。

I wish to change the password of an account.我想更改帐户的密码。 This is my code right now:这是我现在的代码:

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")
            }

Every time I tap the "Update password" button, the application crashes.每次我点击"Update password"按钮时,应用程序都会崩溃。 That's because credential is nil.那是因为credential为零。 So I did my research and found the method EmailAuthProvider() .所以我做了我的研究并找到了方法EmailAuthProvider() So I added this line of code:所以我添加了这行代码:

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

For ethical reasons and best practice, I do not store user password within my database.出于道德原因和最佳实践,我不在我的数据库中存储用户密码。 So how can I even get/access the current user's password?那么我怎样才能获得/访问当前用户的密码呢? Without storing it?不存储它?

I guess I can get the user to enter their current password which would allow for reauthentication, but that is there a way in which I can do it whilst the view loads?我想我可以让用户输入他们当前的密码,这样可以重新进行身份验证,但是有没有一种方法可以在加载视图时执行此操作? So they're not required to hit the update password button twice?所以他们不需要按两次更新密码按钮? And for better UI experience.并获得更好的 UI 体验。 They user is already logged in, and are not required to login unless they sign out.他们的用户已经登录,除非他们注销,否则不需要登录。

OK, so to ensure I always had authenticated the user I demanded the user input their log in credentials prior to even getting the chance/view to update their credentials.好的,为了确保我始终对用户进行身份验证,我要求用户在有机会/查看更新其凭据之前输入他们的登录凭据。

My solution:我的解决方案:

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

So how can I even get/access the current user's password?那么我怎样才能获得/访问当前用户的密码呢? Without storing it?不存储它?

You can't.你不能。 Firebase Authentication manages the password entirely and it doesn't provide a way to get that string. Firebase 身份验证完全管理密码,它不提供获取该字符串的方法。 Doing so would be a security issue.这样做将是一个安全问题。

If you want to let the user change their password, you can call updatePassword() on the user object. You'll notice that it does not require the current password.如果您想让用户更改他们的密码,您可以对用户 object 调用updatePassword() 。您会注意到它不需要当前密码。 The fact that the user is already signed in is proof that they own the account.用户已经登录的事实证明他们拥有该帐户。 I suggest also reading the documentation on setting a user's password .我还建议阅读有关设置用户密码的文档

If the case you're trying to handle is a forgotten password, there is a different process for that by sending a password reset email .如果您要处理的情况是忘记了密码,则可以通过发送密码重置 email来进行不同的处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我无法获取用户的 AWS Cognito 凭证 (swiftUI) - I cannot get the AWS Cognito credentials of a user (swiftUI) 具有匿名凭据的 Firestore 模拟器 - Firestore emulator with anonymous credentials 如何在 SwiftUI 中使用 Firebase Auth 登录后检查用户是否已存在于 Firestore 集合中 - How to check if a user already exists in a Firestore collection after signing in with Firebase Auth in SwiftUI 使用 SwiftUI 在 Firestore 中匹配 collections - Matching collections in Firestore using SwiftUI SwiftUI 中具有实时更新结果的 Firestore Geohash 查询 - Firestore Geohash Query with Live Updating Results in SwiftUI Imageurl 未保存到 Firestore 数据库 - SwiftUI - Imageurl not getting saved to Firestore database - SwiftUI 从 swiftui 中的云 firestore 数组中删除元素 - delete element from cloud firestore array in swiftui 从子集合 Firestore 中获取文档 swiftUI - getting documents from a subcollection Firestore swiftUI 错误:无法加载默认凭据(Firebase function 到 firestore) - Error: Could not load the default credentials (Firebase function to firestore) SwiftUI 和 Firestore - 启动时从不同 collections 读取多个文档的逻辑 - SwiftUI and Firestore - logic for reading multiple documents from different collections at startup
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM