简体   繁体   English

SwiftUI 和 Firebase:当前用户是否持续存在?

[英]SwiftUI & Firebase: Does current user persist?

I am building an app with firebase login, and when I check for the current user, even when I start the simulator it will say that there is a current user.我正在构建一个带有 firebase 登录的应用程序,当我检查当前用户时,即使我启动模拟器,它也会说有一个当前用户。 Is this correct and does the user persist?这是正确的,用户是否坚持?

Here is where I check for the user:这是我检查用户的地方:

var body: some View {
        VStack{
            if authManager.signedIn {
                //Create a coordinator instance and inject it into the app
                HomeTabView()
            } else {
                InitialView()
            }
        }
        .onAppear{
            print(authManager.signedIn)
            authManager.signedIn = authManager.isSignedIn
            print(authManager.signedIn)
        }
    }

And here is the code for the auth manager:这是身份验证管理器的代码:

class authentication: ObservableObject {
    //Class that will handle logging in using firebase
    
    let auth = Auth.auth()
    @Published var signedIn: Bool = false
    private var firebaseLoggedIn: Bool = false
    private var localLoggedIn: Bool = false
    //Enabled will be triggered through the settings to allow the user the opportunity to use biometrics to login
    var enabled: Bool = false
    
    var isSignedIn: Bool {
        if auth.currentUser != nil {
            print (auth.currentUser?.email as Any)
        }
        return auth.currentUser != nil 
    }

Firebase automatically persists user credentials and tries to restore them when the app restarts. Firebase 会自动保留用户凭据,并在应用重新启动时尝试恢复它们。 Note that this requires a call to the server, so it may not be done right when your app starts and so currentUser may still be nil at that point.请注意,这需要调用服务器,因此在您的应用程序启动时可能无法正确完成,因此此时currentUser可能仍然nil

To ensure you get notified when the user credentials have been restored (or rejected by the server, such as when the account has been disabled), be sure to use an auth state listener as shown in the first snippet in the documentation n on getting the current user .为了确保当用户凭据已恢复你收到此通知(或服务器拒绝,例如当帐户已被禁用),请务必使用一个auth状态侦听器,如在文档中的第一个片段N于获得当前用户

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM