title: Navigate after successful login with SwiftUI
body: <p>I'm using Firebase and SwiftUI to log in as a user. I have everything working but can't figure out how to navigate to the next page once the user is successfully logged in. NavigationLinks seem weird to me so I'm wondering if there's any other way to navigate to the next view. The <code>LoginView</code> contains the login screen and the <code>LoginViewModel</code> does the sign-in procedure with Firebase. The issue is that if I return views based on a variable in the root view, it doesn't change the view once any view other than the root view is opened (i.e if I navigate to the Login view, it won't go to the main view after the login button is pressed).</p>
  <p>ContentView (The root view displaying all the other views):</p>
  <pre><code>import SwiftUI
  
  struct ContentView: View {
      @StateObject var userLoggedIn = LoginViewModel()
      var body: some View {
          if !userLoggedIn.isLoggedIn {
              LoginView()
          }
          else {
              MainView()
          }
      }
  }
  </code></pre>
  <p>LoginView:</p>
  <pre><code>import SwiftUI
  import FirebaseAuth
  
  struct LoginView: View {
      
      @State private var email: String = &quot;&quot;
      @State private var password: String = &quot;&quot;
      @State private var isEditing = false
      @State private var showPassword = false
      @State private var radius = 300
      
      private var canLogIn: Bool {
          return !email.isEmpty &amp;&amp; !password.isEmpty
      }
  
      let loginView = LoginViewModel()
      
      @ViewBuilder
      var body: some View {
          
          return NavigationView(content: {
              
              VStack {
                  Spacer()
                      .frame(height: 150)
                  
                  Text(&quot;PET SUPPORT&quot;).foregroundColor(Color.petSupportText)
                      .font(Font.custom(&quot;Permanent Marker&quot;, size: 36))
                      .padding()
  
                  Group {
                      
                      HStack {
                          
                          Text(&quot;EMAIL&quot;)
                              .font(Font.custom(&quot;Permanent Marker&quot;, size: 18))
                              .padding(.top, 10)
                          
                          Spacer()
                          
                      }
                      
                      TextField(&quot;Email&quot;, text: $email) {
                          isEditing in self.isEditing = isEditing
                      }
                      .autocapitalization(.none)
                      .keyboardType(.emailAddress)
                      .disableAutocorrection(true)
                      .padding(.top, 20)
                      
                      Divider()
                          .foregroundColor(.black)
                      
                  }
  
                  
                  Group {
                      
                      HStack {
                          
                          Text(&quot;PASSWORD&quot;)
                              .font(Font.custom(&quot;Permanent Marker&quot;, size: 18))
                              .padding(.top, 10)
                          
                          Spacer()
                          
                      }
  
                      ZStack {
                          if showPassword {
                              TextField(&quot;Password&quot;, text: $password)
                          }
                          else {
                              SecureField(&quot;Password&quot;, text: $password)
                          }
                      }
                      .frame(height: 20)
                      .autocapitalization(.none)
                      .overlay(Image(systemName: showPassword ? &quot;eye.slash&quot; : &quot;eye&quot;).onTapGesture { showPassword.toggle() }, alignment: .trailing)
                      .disableAutocorrection(true)
                      .padding(.top, 20)
                      
                      Divider()
                          .foregroundColor(.black)
                      
                  }
                  
                  Spacer();
                  
                  Group {
                      
                      Button(action: {
                              loginView.login(email: email, password: password)
                              radius = 2000
                              MainView()
                      }, label: {
                          Text(&quot;Login&quot;)
                      })
                          .foregroundColor(.white)
                          .font(Font.custom(&quot;Permanent Marker&quot;, size: 18.0))
                          .padding(.horizontal, 20)
                          .padding()
                          .background(Color.petSupportBlue)
                          .cornerRadius(70.0)
                          .disabled(!canLogIn)
                  }
                  
                  Spacer()
              }
              .padding(.horizontal, 30)
              .ignoresSafeArea()
              
          })
          
      }
  }
  </code></pre>
  <p>LoginViewModel:</p>
  <pre><code>import Foundation
  import Firebase
  
  class LoginViewModel: ObservableObject {
      
      @Published var isLoggedIn = false
      func login(email: String, password: String) {
          
          Auth.auth().signIn(withEmail: email, password: password) { (result, error) in
              
              if let error = error {
                  print(error.localizedDescription)
              } else {
                  print(&quot;Logged In!&quot;)
                  self.isLoggedIn = true
              }
          }
          
      }
      
  }
  </code></pre>
  <p>MainView:</p>
  <pre><code>import SwiftUI
  
  struct MainView: View {
      var body: some View {
          Text(&quot;Hello, World!&quot;)
      }
  }
  </code></pre>