0

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.

0

1 Answer 1

1

Your function passwordResetRequest is returning immediately with, status = false and errorMessage = "0". passwordResetRequest is an async function, so try something like this:

func passwordResetRequest(email: String,
                          onSuccess: @escaping() -> Void,
                          onError: @escaping(_ errorMessage: String) -> Void) {
    
    Auth.auth().sendPasswordReset(withEmail: email) { error in
        if error != nil {
            onError(error!.localizedDescription)
        } else {
            onSuccess()
        }
    }
}

and call it like this in your Button action:

        passwordResetRequest(email: email, onSuccess: {
            print("---> onSuccess")
        }) { error in
            print("---> error: \(error)")
        }
Sign up to request clarification or add additional context in comments.

1 Comment

For the call on the button I used: Button(action: { user.passwordResetRequest(email, onSuccess: { showingResetPasswordAlert = true }, onError: { err in self.errorMsg = err print(errorMsg) showingErrorMsg = true } ) }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.