Switch View after "Login"

Good day,

before I start, I want to say iam pretty new in SwiftUI. As the title says, I want to change the view after I loggend in with a button click.

So the login is bounded on a condition that login process itself is successful.

But it seems, I have a misconception in my brain about the things works in swiftUI, because if I click on the button, the swiftUI view doesn't change anymore. So the reason is the condition.

Here is the Code and thanks in advance

    
    @State private var email = ""
    @State private var password = ""
    @State private  var isLoggedIn: Bool = false
    @StateObject var user = User(usermail: "")
.........
NavigationStack {
            ZStack {
                
                BackgroundView()
                
                VStack {
                    LogoView()
                        .padding(.bottom, 25)
                    
                    VStack(spacing: 20){
                        EmailField(text: $email)
                            .padding(.horizontal, 32)
                        PasswordSecureField(text: $password, placeholder: "Password")
                            .padding(.horizontal, 32)
                        Text(self.status)
                        
                        
                        **HStack {
                            Spacer()

                        Button(action: { login() }) {
                            
                            if self.isLoggedIn == true {
                                
                                NavigationLink(destination: MainMenuView()
                                    .navigationBarHidden(true),
                                               label: {
                                    AuthenticationButtonView(text: "Sign in")
                                }
                                               
                                ).padding()
                            }
                            else {
                                AuthenticationButtonView(text: "Sign in")
                            }
                        }**



func login() {
            Auth.auth().signIn(withEmail: email, password: password) { (result, error) in
                if error != nil {
                    print(error?.localizedDescription ?? "")
                    self.status = "Bad Credentials or not registered"
                } else {
                    self.isLoggedIn = true
                    print("Login sucessfully")
                    user.usermail = email
                    
                   
                }
            }
        }
Answered by CodeTalksToMe in 753152022

Got it.

A minimal example cleared it.

    
    @State var isLoggedIn: Bool = false
    
    var body: some View {
        VStack {
            NavigationStack {
                Button("LOGIN"){
                    login()
                }
                .navigationDestination(isPresented: $isLoggedIn) {
                              Page2()}
            }
            
        }
    }
    
    func login(){
        self.isLoggedIn.toggle()
    }
}

Sorry. I want to add something:

I just want to go to Main Menu after a successful login. I found some solutions with NavigationLink and the parameter isActive. Unfortunately this parameter is deprecated.

Accepted Answer

Got it.

A minimal example cleared it.

    
    @State var isLoggedIn: Bool = false
    
    var body: some View {
        VStack {
            NavigationStack {
                Button("LOGIN"){
                    login()
                }
                .navigationDestination(isPresented: $isLoggedIn) {
                              Page2()}
            }
            
        }
    }
    
    func login(){
        self.isLoggedIn.toggle()
    }
}
Switch View after "Login"
 
 
Q