You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firebase登录后无法识别当前用户及登出注册新用户无UID问题

Hey David, let's break down your two Firebase Auth issues one by one:

问题一:Firebase登录后为何无法识别当前用户?

There are a few common reasons this might happen, along with fixes:

  • You're checking currentUser before the async login finishes
    Firebase's login methods (like signIn(withEmail:password:)) run asynchronously. If you try to grab Auth.auth().currentUser right after calling the login function, the network request hasn't completed yet, so the user state won't be updated. Always handle user logic inside the login completion closure, or use a state listener.

  • You haven't set up an Auth state listener
    Firebase recommends using addStateDidChangeListener to track real-time user state changes (login, logout, token expiration, etc.). This is more reliable than checking currentUser manually. Here's how to implement it:

    Auth.auth().addStateDidChangeListener { auth, user in
        if let user = user {
            // User is signed in, handle your logic here
            print("Current user UID: \(user.uid)")
        } else {
            // No user is signed in
            print("No active user")
        }
    }
    
  • Local cache glitches
    Sometimes the local Auth cache can get out of sync. Try uninstalling and reinstalling your app, or clearing its stored data, then test login again.

  • Incorrect project configuration
    Double-check that your GoogleService-Info.plist is properly added to your iOS project, and that the Bundle ID in your plist matches the one registered in the Firebase Console. Mismatched configs can break Auth state sync.

问题二:登出后注册新用户无法获取UID

This is a classic async timing issue! Your code tries to grab currentUser immediately after calling createUser(withEmail:password:), but createUser runs asynchronously. The network request to create the user hasn't finished yet when you check currentUser, so it returns nil.

Fix: Move UID logic inside the createUser completion closure

Only when the completion closure fires do you know the user has been successfully created (and automatically signed in by Firebase). Here's your revised code:

guard let email = emailTextField.text, let password = passwordTextField.text else { 
    print("Form is not valid") 
    return 
} 

Auth.auth().createUser(withEmail: email, password: password) { authResult, error in
    if let error = error {
        print("Registration failed: \(error.localizedDescription)")
        return
    }
    
    // This is the safe place to access the new user's UID
    guard let uid = authResult?.user.uid else {
        print("No UID")
        return
    }
    
    print("Account creation successful, UID: \(uid)")
}

Pro tip: Use authResult?.user directly instead of relying on currentUser here—it's the direct result of the registration request, so it avoids any potential state sync delays.

内容的提问来源于stack exchange,提问作者David Sung

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:12:48