简体   繁体   中英

Swift: Firebase read permission denied

So I'm building this app in swift, which has data that should be read before user logging in.

To secure it from being read from out of my app, I put up the basic security rule:

".read": "auth != null"

To prevent read before logging in, I put up a loading view controller while calling:

func application(application: UIApplication, willFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {

    let ref = Firebase(url: "<my firebase url>")

    ref.observeAuthEventWithBlock({ authData in
        if authData != nil {
            segueToListViewController()
        }
    })
    return true
}

But when I get to such point, sometimes firebase gives me "permission denied" error, sometimes it doesn't.

I'm sure there's a logged user, because I'm logging everything:

  • Login changed
  • User logged in AuthData facebook:
  • applicationDidBecomeActive
  • Loaded ListViewController
  • Loading Data
  • Error: Error Domain=com.firebase Code=1 "Permission Denied"

So why does Firebase sometimes denies reading when the user is logged in?

I'm pretty late to this party, but it might help if you put the .read rule one level higher, directly under the child's name.

For example

{
  "rules": {
    "users": {
      "$uid": {
        ".read": "auth != null && auth.uid == $uid", 
        ".write": "auth != null && auth.uid == $uid"
      }
    },
    "child": {
        ".read": "auth != null",
        ".write": "auth != null", 
      "$child": {
      }
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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