I want to display the error message to the user if there is an error with signing in. I created a state var called loginStatusMessage and added it in the signUp function and passed it to the view where i want to display the message but it is not showing up in the view.
AppViewModel
@State var loginStatusMessage = ""
`func signUp(email: String, password: String) {
auth.createUser(withEmail: email, password: password) { [weak self] result, error in
if let error = error {
print("Failed to create user:", error)
self?.loginStatusMessage = "Failed to create user: \(error)"
return
}
print("Successfully created user: \(result?.user.uid ?? "")")
self?.loginStatusMessage = "Successfully created user: \(result?.user.uid ?? "")"
// Success
DispatchQueue.main.async {
self?.signedIn = true
}
}
}`
CreateAccountView
Button {
guard !email.isEmpty, !password.isEmpty else {
return
}
viewModel.signUp(email: email, password: password)
} label: {
Text("Create Account")
}
.frame(height: 40)
.foregroundColor(.white)
.background(Color(hue: 0.454, saturation: 0.991, brightness: 0.52, opacity: 0.911))
.buttonStyle(.bordered)
.cornerRadius(5)
.padding(.vertical)
Text(self.viewModel.loginStatusMessage)
.foregroundColor(.red)