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()
}
}
}