IrishGringo IrishGringo - 3 months ago 14
Android Question

firebase rules, android, hardcoding write credentials into App so there is no Login

I am working on an ANDROID/FIREBASE app that will run on 5 to 10 phones. It's for our truck drivers, and just for private use. These Drivers have never had a phone let alone an email before, so not tech savy to remember their login credentials. They barely speak English.

SO I want to hard code some kind of authentication method into the ANDROID app so there is no real login, but I can still write to the Firebase DB. I do not want to leave this open to the world, so I do want to have write rules. I also want to do a WEB app with read, write. It's an unusual case, but I can think of other cases like this. I have another project where I want to control READ

Ive seen this answer... (but its 3 years old)

I was thinking of doing something like..

Function AllowTruckDriversWrite(TDID){
if one of these {td01, td02, td03}
return true
else return false
}

{
"rules": {
".read": true,
".write": AllowTruckDriversWrite(ValFromAndroidApp)
}
}


But I am not sure how to pass ValFromAndroidApp from phone to the rules engine.

I was also thinking of using a VALIDATE
".validate": AllowTruckDriversWrite(ValFromAndroidApp)

I am still experimenting with these rules... looking for more documentation on them. Does anyone have some suggestions, Ideas, thoughts on how to setup RULES for something like this? Perhaps someone could clean up the rules code? many thanks

Answer

I believe it would be a better if you actually create a user for them, take their phone, login into app but do not provide a signout option in the app. In this way, unless they literally uninstall the app, the app will not signout and you can track every driver. And even if something happens to their phone you can just simply login again on the new phone without loosing any data. Since the app is private the userId (uid) of every user is up to you and you can have these rules :

"users":{
  "$uid":{
    ".read": true,
    ".write": "auth.uid == $uid"

  }
}

or maybe :

"users":{
  "$uid":{
    ".read": true,
    ".write": "auth.uid != null"

  }
}

I just thought this might help. Hope my answer was not very idiotic. ;-)