How to read/write from one IOS App set up with firebase, to another firebase database contained in another firebase project? Swift 3
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.
Now, from within this IOS app, I would like to access another FireBase Database brevCustomer
. For some reason let dbRef
from viewDidLoad
has a flag in Xcode saying this 'immutable value dbRef
was never used' and the app crashes on the first line in fun startObserving() dbRef.observe(.value, with: { (snapshot: FIRDataSnapshot) in
.
Could anyone show how to do the configuration so that I can read/write to brevCustomer database?
EDIT
Please consider the following scenario:
I have two IOS apps Customer and Worker and two Firebase Projects named CustomerFireBase and WorkerFirebase and I would like them to work in the following way.
Customer registers with email and password, logs in, makes a booking, and data is saved in CustomerFireBase.
- Worker registers with email and password, logs is, observe WorkerFirebase for value changes or child added
- read from CustomerFireBase
- write to CustomerFireBase
- write to WorkerFirebase
- read from CustomerFireBase
How can I achieve this? Basically, I need to get read/write access from one IOS app configured in the usual way with Firebase, to another Firebase Database contained in another Firebase project.
Class Claim {
var dbRef:FIRDatabaseReference! //create a reference to Firebase database `brevCustomer`, not the one from .plist file
override func viewDidLoad() {
super.viewDidLoad()
let app = FIRApp(named: "brevCustomer")
let dbRef = FIRDatabase.database(app: app!).reference().child("Users")
startObservingDB() // observe the database for value changes
}
func startObservingDB() {
//it crashes on the line below
dbRef.observe(.value, with: { (snapshot: FIRDataSnapshot) in
//iterate over each user node child
for user_child in snapshot.children {
print(user_child)}
}, withCancel: { (Error: Any) in
print(Error)
})
} // end of startObservingDB()
}//end of Claim class
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Use Firebase library to configure APIs for the initial project with .plist file saved in Xcode
FIRApp.configure()
/** 1. create a Firebase options object to hold the configuration data for the second Firebase Project */
let secondaryOptions = FIROptions(googleAppID: "1:82424687545:ios:71df5d45218ad27",
bundleID: "com.vivvdaplar.Brev",
gcmSenderID: "8201647545",
apiKey: "AIzaSyCNtyUf2T3UunH6-ci_WyvOqCl_RzXI",
clientID: "8200687545-42vklp94reavi6li6bolhcraoofc6.apps.googleusercontent.com",
trackingID: nil,
androidClientID: nil,
databaseURL: "https://brev-72e10.firebaseio.com",
storageBucket: "com.vivvdaplar.Brev",
deepLinkURLScheme: nil)
// Configure the app
FIRApp.configure(withName: "brevCustomer", options: secondaryOptions!)
return true
}
} //end of AppDelegate
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: "[email protected]", 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'"