In my UserViewModel, I have a passwordreset function. This works, but I want it to return to my mainview information about the error, or if it was successful. Sadly just the default values are returned, and then the values get updated in the function itself, after the fact.
func passwordResetRequest(_ email: String) -> (successful: Bool, errorMsg: String) {
var status = false
var errorMessage = "0"
Auth.auth().sendPasswordReset(withEmail: email) { error in
if error != nil {
errorMessage = error?.localizedDescription ?? "unknown error"
print("In UserViewModel.PasswordResetRequest(): \(errorMessage)")
status = false
} else {
print("success")
status = true
}
}
return (status,errorMessage)
}
Here is the code from the view :
VStack(spacing: 13){
HStack(spacing: 0) {
Text("Forget your password? ")
Button(action: {
let result = user.passwordResetRequest(email)
print(result)
if (result.successful) {
showingResetPasswordAlert = true
} else {
self.errorMsg = result.errorMsg
print(result.errorMsg)
print(errorMsg)
print("from button^")
showingErrorMsg = true
}
}, label: {
Text("Reset Password")
.font(.callout)
.foregroundColor(.black)
.bold()
}).alert(isPresented: $showingResetPasswordAlert) {
Alert(title: Text("Important message"), message: Text("Password Reset Email will be sent to \(email)"), dismissButton: .default(Text("Got it!")))
}
.alert(isPresented: $showingErrorMsg) {
Alert(title: Text("Important message"), message: Text("Error: \(self.errorMsg) occured during the password reset for the email: \(email)"), dismissButton: .default(Text("Got it..")))
}
}
Here is the console output:
(successful: false, errorMsg: "0")
0
0
from button^
In UserViewModel.PasswordResetRequest(): An email address must be provided.
So, the problem is line 6 of the passwordResetRequest doesn't seem to be working, or it seems that the return statement is being called before this block runs.