swift 我注销后Firebase登录不起作用

vtwuwzda  于 2023-03-11  发布在  Swift
关注(0)|答案(1)|浏览(115)

因此,第一次登录Swift UI应用程序时运行良好。我登录后,视图从登录视图更改为主视图。然而,在我注销后,登录不会导致视图更改,所以我认为它不起作用。
这是我的AuthViewModel,我在其中处理登录和注销。

import FirebaseAuth

class AuthViewModel: ObservableObject {
    static let shared = AuthViewModel()

    private(set) var authSubscription: AuthStateDidChangeListenerHandle?
    @Published var currentUser: User?
    @Published var error: Error?

    init() {
        authSubscription = Auth.auth().addStateDidChangeListener { [weak self] (auth, user) in
            self?.currentUser = user
        }
    }

    func signIn(email: String, password: String) {
        Auth.auth().signIn(withEmail: email, password: password) { [weak self] (authResult, error) in
            if let error = error {
                self?.error = error
            } else {
                self?.error = nil
            }
        }
    }

    func signUp(email: String, password: String) {
        Auth.auth().createUser(withEmail: email, password: password) { [weak self] (authResult, error) in
            if let error = error {
                self?.error = error
            } else {
                self?.error = nil
            }
        }
    }

    func signOut() {
        do {
            try Auth.auth().signOut()
        } catch let error {
            self.error = error
        }
    }
}

这里还介绍了如何从注册视图切换到主视图

struct ContentView: View {
    @State var screenOn = 0
    @EnvironmentObject var authViewModel: AuthViewModel

    var body: some View {
        if let user = authViewModel.currentUser {
            if screenOn == 0 {
                someView(screenOn: $screenOn)
                    .onAppear(perform: UIApplication.shared.addTapGestureRecognizer)
                    .onOpenURL { url in
                        let link = url.absoluteString

                      
                    }
            } else if screenOn == 1 {
                someView(screenOn: $screenOn)
                    .onAppear {
                        authViewModel.signOut()
                    }
            } else if screenOn == 3 {
                someView(screenOn: $screenOn)
            } else if screenOn == 4 {
                someView(screenOn: $screenOn)
            }
        } else {
            Loginpage()
        }
    }
}
qnakjoqk

qnakjoqk1#

The **ScreenOn** variable is not updated correctly after calling the signOut fun.

func signOut() 
{
        do {
            try Auth.auth().signOut()
             self.screenOn = 0  // Add this line 
        } catch let error {
            self.error = error
        }
    }

Add a breakpoint to verify that the correct view is being loaded.

相关问题