I'm trying to be able to update at my password on Firebase, but it's not working yet. Hope you can help me to solve this problem
Form
struct ChangePassword: View {
@State var oldPassword: String = ""
@State var newPassword: String = ""
@State var confirmPassword : String = ""
@ObservedObject var loginViewModel = LoginViewModel()
var body: some View {
ZStack {
VStack {
HStack {
Text("Old Pw")
.poppinsRegularFont(size: 17)
.foregroundColor(Color(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1))).opacity(1)
SecureField("", text: $oldPassword)
.padding()
.font(Font.system(size: 15, weight: .medium, design: .serif))
.frame(width: UIScreen.main.bounds.width - 80,height: 48)
.background(
Color.black.opacity(0.1)
)
}
.padding(.bottom,20)
HStack {
Text("New Pw")
.poppinsRegularFont(size: 17)
.foregroundColor(Color(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1))).opacity(1)
SecureField("", text: $newPassword)
.padding()
.font(Font.system(size: 15, weight: .medium, design: .serif))
.background(
Color.black.opacity(0.1)
)
}
.padding(.bottom,20)
HStack {
Text("Confirm Pw")
.poppinsRegularFont(size: 17)
.foregroundColor(Color(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1))).opacity(1)
SecureField("", text: $confirmPassword)
.padding()
.font(Font.system(size: 15, weight: .medium, design: .serif))
.background(
Color.black.opacity(0.1)
)
}
Button(action: {
loginViewModel.updatePassword(password: confirmPassword)
}) {
Text("Save")
}
}
}
}
}
func UpdatePassword
class LoginViewModel: ObservableObject {
let userPassword = Auth.auth().currentUser
var credential: AuthCredential?
func updatePassword(password: String) {
if let credential = credential {
userPassword?.reauthenticate(with: credential ) { error,_ in
if error != nil {
// An error happened.
} else {
Auth.auth().currentUser?.updatePassword(to: password) { (error) in
// ...
}
}
}
}
}
}
I'm trying to be able to update at my password on Firebase, but it's not working yet. Hope you can help me to solve this problem...................................
let user = Auth.auth().currentUser
let credential = EmailAuthProvider.credential(withEmail: user!.email!, password: currentPassword)
user?.reauthenticate(with: credential, completion: { (authResult, error) in
if let error = error {
// Handle re-authentication error
return
}
user?.updatePassword(to: newPassword, completion: { (error) in
if let error = error {
// Handle password update error
return
}
// Password update successful
})
})
You don't need to reauthenticate user, please write below code
Auth.auth().currentUser?.updatePassword(to: newPasswordTF.text!) { (error) in
if let error = error as NSError? {
}
// Password change sucussfully
// ...
}
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.