Skip to content

Instantly share code, notes, and snippets.

View hiranya911's full-sized avatar

Hiranya Jayathilaka hiranya911

View GitHub Profile
// This version of the application code was written for the V1 schema.
const val SUPPORTED_SCHEMA_VERSION = 1
private val db = FirebaseFirestore.getInstance()
private val remoteConfig = FirebaseRemoteConfig.getInstance()
private val schemaMappings = mutableMapOf<String,JsonObject>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
remoteConfig.fetchAndActivate()
service cloud.firestore {
match /databases/{database}/documents {
match /people/{person} {
allow write: if request.resource.data.familyName is string
|| request.resource.data.surname is string;
}
}
}
// Latest security rules look for a `familyName` field instead of `surname`. This
// function will not therefore work.
fun addPerson(firstName: String, surname: String): Task<Void> {
val data = hashMapOf(
"firstName" to firstName,
"surname" to surname
)
return db.collection("people").document()
.set(data)
}
fun addPerson(firstName: String, familyName: String): Task<Void> {
val data = hashMapOf(
"firstName" to firstName,
"familyName" to familyName
)
return db.collection("people").document()
.set(data)
}
fun getPerson(personId: String) {
service cloud.firestore {
match /databases/{database}/documents {
match /users/{uid} {
allow write: if request.resource.data.age >= 18
&& request.resource.data.email.matches('.+@.+')
&& request.resource.data.screenName is string
&& get(/databases/$(database)/documents/screenNames/$(request.resource.data.screenName)).data.uid == uid
&& exists(request.resource.data.company);
}
}
db.collection('cities').get().then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
const data = doc.data();
display(data);
if (closeToMillion(data)) {
console.log(`${dat.name} is close to reaching 1 mil citizens.`);
}
});
});
service cloud.firestore {
match /databases/{database}/documents {
match /cities/{city} {
allow write: if int(request.resource.data.population) > 0
&& request.resource.data.name is string
&& request.resource.data.name.size() > 0
&& request.resource.data.name.size() <= 32;
}
}
}
service cloud.firestore {
match /databases/{database}/documents {
match /cities/{city} {
allow write: if request.resource.data.population > 0
&& request.resource.data.name is string
&& request.resource.data.name.size() > 0
&& request.resource.data.name.size() <= 32;
}
}
}
data class City(
val name: String? = null,
val state: String? = null,
val country: String? = null,
val isCapital: Boolean? = null,
val population: Long? = null,
val regions: List<String>? = null
)
db.collection("cities")
// Specify the unique ID of the tenant to authenticate.
firebase.auth().tenantId = 'TENANT_ID1';
// Usual Firebase sign-in logic. User will be authenticated
// with tenant-specific identity providers.
firebase.auth().signInWithEmailAndPassword(email, password)
.then((result) => {
const user = result.user;
// user.tenantId is set to 'TENANT_ID1'.
});