M3L4 : Associating Data to the User

In the final lesson from module 3 of the CWC+ iOS Databases course, Chris talks us through setting up Firebase, and our Xcode app, to handle associating data with a user.

The primary point of note is that we’ve previously been setting up Cloud Firestore in “test mode”. This can be thought of as allowing anyone who has the ID access to the database, and access is automatically disabled after 30 days.

Now we’re moving onto “production mode”, which comes with a set of rules intended to deny everyone access.

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if false;
    }
  }
}

The rules effectively says “allow read & write access if the following condition is ‘true'” but, because the condition (after the ‘if’) just states “false” then it’s “not true”, therefore no one gets read & write access.

To allow access, the condition needs to be changed to something like this:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth != null;
    }
  }
}

Now, read & write access is allowed if the request is authenticated. The rules can be made more specific, but this is what we’re working with right now.

The user’s ID is now associated with his own unique document within the collection.

Extra notes:

  • Our Podfile now contains –
    • pod 'Firebase/Auth'
    • pod 'Firebase/Firestore'
  • In addition to existing imports, the relevant Swift files in our product call up –
    • import FirebaseFirestore

Another point of note is that, as we’ve experienced before, technology moves on and things change.

The tutorial helpfully takes us through setting up our Firestore with CocoaPods again, but the actual screen we get from Cloud Forestore doesn’t want us to use CocoaPods any more and, instead, directs us towards the Swift Package Manager, which we don’t know anything about.

The Dark Ages (last year)
The Modern Era (probably deprecated by the time you read this!)

The link provided by the Firestore webpage is highly confusing, and seems to rely on you already knowing enough about Swift Package Manager that you don’t actually need the instructions provided. Not very intuitive or helpful for newbies like me.

I can see that I’m going to have to read up on the Swift Package Manager and use that whenever Chris talks about CocoaPods again.

But, for now, I’ll leave it there as this concludes Module 3 and I’m eager to move onto the next module which should see what we’ve learned being put to practical applications, such as our previous CWC+ apps.