I am in a login page and when I press the login button, if the login was completed without errors I want to navigate to a new view, if the login failed I want to stay on the same view. Here is my code:
struct LogIn: View {
@State var mesaj = ""
@State private var email = ""
@State private var password = ""
@EnvironmentObject var model: Model
var body: some View {
NavigationView {
VStack {
// EMAIL
TextField("E-mail", text: $email).textFieldStyle(RoundedBorderTextFieldStyle())
// PASSWORD
TextField("Password", text: $password).textFieldStyle(RoundedBorderTextFieldStyle())
// this is the login button
Button(action: {
loginRequest(link: "https://someapi.com", email: self.email, password: self.password) // this is the login request
let seconds = 5.0
DispatchQueue.main.asyncAfter(deadline: .now() + seconds) {
self.mesaj = message
if message == "You are now logged in"
{
// here I want to navigate to a new view
}
}
}) {
Text("LogIn")
}
Text(mesaj)
Spacer()
}
}
}
}
Thanks!