How to read/write from one IOS App set up with firebase, to another firebase database contained in another firebase project? Swift 3

前端 未结 1 1318
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-02 02:50

I have a Firebase database connected to my IOS app with the GoogleService-Info.plist. In AppDelegate I configured the App FIRApp.configure(). I could read/write data.

相关标签:
1条回答
  • 2020-12-02 03:47

    Responding the question and comments.

    As you know, when a user registers with Firebase, a user account is created on the Firebase server and the user is provided a user id (uid).

    A typical design pattern is to have a /users node in Firebase that stores other information about the user, such as a nickname, address or phone number.

    We can leverage that /users node to also indicate what kind of user it is; Worker or Client, which would tie into the rest of the app and Firebase so they get to the correct data.

    For example

    users
      uid_0
        nickname: "John"
        user_type: "Worker"
      uid_1
        nickname: "Paul"
        user_type: "Client"
      uid_2
        nickname: "George"
        user_type: "Worker"
      uid_3
        nickname: "Ringo"
        user_type: "Worker"
    

    As you can see, John, George and Ringo are all workers and Paul is a client.

    When the user logs in, the Firebase signIn function will return the users auth data, which contains the uid.

        Auth.auth().signIn(withEmail: "paul@harddaysnight.com", password: "dog",
         completion: { (auth, error) in
    
            if error != nil {
                let err = error?.localizedDescription
                print(err!)
            } else {
                print(auth!.uid)
                //with the uid, we now lookup their user type from the
                //users node, which tells the app if they are a client
                //or worker
            }
        })
    

    If the app data is divided like this

    app
      client_data
         ...
      worker_data
         ...
    

    A simple rule could be set up that verifies the users user_type is Worker for the worker_data node and Client for the client_data node. Here's a pseudo example that will allow a Client user to only access the data in the client_data node (conceptual)

    rules 
      client_data
         $user_id
            ".read": "auth != null && root.child(users)
                                          .child($user_id)
                                          .child("user_type") == 'Client'"
    
    0 讨论(0)
提交回复
热议问题