You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Firebase Storage上传图片时遭遇未处理异常:[firebase storage/unauthenticated] 用户未认证,请认证后重试

Fixing Firebase Storage "User Not Authenticated" Error

Hey there, let’s work through your Firebase Storage upload issue step by step—no confusing jargon, just practical steps.

First: The Core Issue – User Authentication

That "user not authenticated" error almost always ties back to one thing: your Firebase Storage rules require an authenticated user, but your app isn’t sending a valid authenticated session when uploading. Here’s how to fix that:

  • Make sure Firebase Auth is initialized and the user is logged in
    Before you attempt any upload, confirm that a user has successfully signed in via Firebase Auth (email/password, Google Sign-In, etc.). If the user isn’t logged in, the Storage rules will block the request.

    For example, in Android (Kotlin):

    // Check if user is logged in before uploading
    val currentUser = FirebaseAuth.getInstance().currentUser
    if (currentUser == null) {
        // Redirect to your login screen first
        val intent = Intent(this, LoginActivity::class.java)
        startActivity(intent)
        return // Don't proceed with upload until user logs in
    }
    
    // Proceed with Storage upload
    val storageRef = FirebaseStorage.getInstance().reference.child("images/${currentUser.uid}/photo.jpg")
    // ... rest of your upload code
    

    In iOS (Swift):

    guard let currentUser = Auth.auth().currentUser else {
        // Send user to login screen
        let loginVC = LoginViewController()
        navigationController?.pushViewController(loginVC, animated: true)
        return
    }
    
    // Start upload
    let storageRef = Storage.storage().reference().child("images/\(currentUser.uid)/photo.jpg")
    // ... rest of your upload code
    
  • Verify your Firebase Storage rules
    Even if you didn’t share your rules, the most common rule that triggers this error is one that restricts access to authenticated users:

    rules_version = '2';
    service firebase.storage {
      match /b/{bucket}/o {
        match /{allPaths=**} {
          allow read, write: if request.auth != null;
        }
      }
    }
    

    If this is your rule, you must have a logged-in user to upload. For testing only, you can temporarily set allow read, write: if true;—but never leave this for production (it opens your storage to everyone!).

Do You Need App Check?

Short answer: No, App Check isn’t required to fix this authentication error. App Check is an extra security layer to block requests from untrusted apps, but it won’t solve your core "user not authenticated" issue. Focus on getting user authentication working first, then you can add App Check later if you want to harden security.

Any code related to Firebase Auth initialization or user checks should go in a place that runs early in your app’s lifecycle:

  • Android: Put initialization code in your custom Application class (so it runs when the app starts) or your main activity’s onCreate method.
  • iOS: Add it in AppDelegate.swift’s application(_:didFinishLaunchingWithOptions:) method or SceneDelegate.swift if you’re using scene-based navigation.

Quick Troubleshooting Checklist

  1. Double-check that Firebase Auth is enabled in your Firebase Console (go to Authentication > Sign-in method and confirm your chosen method is toggled on).
  2. Test your login flow to ensure the user is actually being authenticated (print currentUser to confirm it’s not null/nil).
  3. Make sure your Firebase SDK versions are up to date—outdated SDKs can cause unexpected authentication issues.

Hope this gets your uploads working smoothly!

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

火山引擎 最新活动