UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Navigate to New View after login

Forums > iOS

Hi I am trying to naviagte to a new view after firebase auth using an if statment but am unable to. It is currently printing the users id from firebase in the concole after authenication, but i would like it to navigate to my home screen. Any help would be much appricated. Current code for the button:

Button{
                    Auth.auth().signIn(withEmail: email, password: password){
                        authResult, error in
                        if let error = error{
                            print(error)
                            return
                        }
                        if let authResult = authResult{
                            print(authResult.user.uid)
                        }
                    }

                }label: {
                    Text("Sign in")
                        .foregroundColor(.white)
                        .font(.title3)
                        .bold()
                        .frame(maxWidth: .infinity)
                        .padding()
                        .background(RoundedRectangle(cornerRadius: 10)
                            .fill(Color(red: 206/255, green: 170/255, blue: 77/255)
                                 )
                                .padding(.horizontal)
                        )
                }

3      

Hi I have found a fix to this for anyone else who is looking for one these are the steps:

  1. Make sure your login view is in a var content: some View
  2. Make a var body: some View and add in if userisloggedIn {Calendar()}else{content}
  3. Add a @State private var userisloggedIn = false
  4. Add in if authResult != nil {userisloggedIn.toggle()}
  5. Then Test!!

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.