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

How to change View after Authentication?

Forums > SwiftUI

Hi All, Im pretty new to SwiftUI, so i hope you can help me out.

My questions is basically , how can i change view/root efter the user has successfully logged in? or logged out? This is the code i use for the facebook authentication

I tried using @Published and connect it to sceneDelegate, but i had to manually reload the app, before the view changed.

Would really appreciate some help -Nick


struct LoginView : UIViewRepresentable {

    func makeCoordinator() -> LoginView.Coordinator {
        return LoginView.Coordinator()
        //        return LoginView.Coordinator(self.tokenLV)

    }

    func makeUIView(context: UIViewRepresentableContext<LoginView>) -> FBLoginButton {
        let button = FBLoginButton()
        button.permissions = ["email"]
        button.delegate = context.coordinator
        return button
    }

    func updateUIView(_ uiView: FBLoginButton, context: UIViewRepresentableContext<LoginView>) {

    }

    class Coordinator : NSObject, ObservableObject,LoginButtonDelegate{
        @Published var  token = AccessToken.current

        func loginButtonDidLogOut(_ loginButton: FBLoginButton) {

            try! Auth.auth().signOut()
            print("Did logout - From loginButtonDidLogOut")

        }

        func loginButton(_ loginButton: FBLoginButton, didCompleteWith result: LoginManagerLoginResult?, error: Error?) {

            if error != nil  {
                print((error?.localizedDescription)!)
                return
            }

            if AccessToken.current != nil {
                let credential = FacebookAuthProvider.credential(withAccessToken: AccessToken.current!.tokenString)
                Auth.auth().signIn(with: credential) {(res ,  er) in

                    if er != nil{
                        print((er?.localizedDescription)!)
                        return
                    }

                    print("SUCCES! ")

                }
            }
        }
    }

}

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    @ObservedObject var loginScreen = LoginView.Coordinator()

    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        if let windowScene = scene as? UIWindowScene {

            let window = UIWindow(windowScene: windowScene)
            if (self.loginScreen.token != nil){ window.rootViewController = UIHostingController(rootView: ContentView())}

            else {window.rootViewController = UIHostingController(rootView:LoginView().frame(width: 250, height: 50)
            .cornerRadius(10) )}

//           window.rootViewController = UIHostingController(rootView: LoginAndMainControler())
            self.window = window
            window.makeKeyAndVisible()

        }
    }
}

3      

There is a way using a Navigationlink isActive parameter. Paul discusses it here;

https://www.hackingwithswift.com/articles/216/complete-guide-to-navigationview-in-swiftui

.. basically, you bind the isActive to a state variable and toggle to true when you want the navigation to take place. This allows you to trigger it in response to another activity completing.

3      

Thanks for directing me in that direction, i read the articel, but i dont see how i would do it in my scenario. Becuase my loggin is as shown above(The first block of code, not the SceneDelegate). Would it be possible for you to write it out. So i can look at the final code and understand it?

Would really really appreciate it

3      

I would look at "Sign in with Apple" rather then FaceBook. You might want to look at Implementing User Authentication with Sign in with Apple

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.