Browse Source

Added the deleteSession cloud function.

wip-firestore
Atridad Lahiji 1 year ago
parent
commit
98903579a9
  1. 12
      database.rules.json
  2. 10
      firebase.json
  3. 4
      firestore.indexes.json
  4. 9
      firestore.rules
  5. 21
      functions/index.js

12
database.rules.json

@ -0,0 +1,12 @@
{
"rules": {
"sessions": {
"$pid": {
"$owneruid": {
".read": "auth != null && auth.uid == $owneruid",
".write": false
}
}
}
}
}

10
firebase.json

@ -1,8 +1,4 @@
{
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
},
"hosting": {
"public": "dist",
"ignore": [
@ -14,6 +10,10 @@
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint"
]
],
"source": "functions"
},
"database": {
"rules": "database.rules.json"
}
}

4
firestore.indexes.json

@ -1,4 +0,0 @@
{
"indexes": [],
"fieldOverrides": []
}

9
firestore.rules

@ -1,9 +0,0 @@
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /sessions/{sessionid} {
allow read, write: if request.auth != null &&
resource.data.owneruid == request.auth.uid;
}
}
}

21
functions/index.js

@ -6,7 +6,7 @@ const rtdb = admin.database();
exports.createSession = functions.https.onCall((data, context) => {
if (!context.auth) {
return { status: 'error', code: 401, message: 'Not signed in' };
return { status: 'error', code: 401, message: 'You are not authorized to perform this action.' };
} else {
return rtdb.ref('/sessions').push({
owneruid: context.auth.uid,
@ -18,3 +18,22 @@ exports.createSession = functions.https.onCall((data, context) => {
})
}
})
/*This function expects an object in the following format:
{
sessionid: Number,
owneruid: Number
}
*/
exports.deleteSession = functions.https.onCall((data, context) => {
if (!context.auth && context.auth.uid != data.owneruid) {
return { status: 'error', code: 401, message: 'You are not authorized to perform this action.' };
} else {
return rtdb.ref(`/sessions/${ data.sessionid }`).remove()
.then(() => {
return { status: 'success', code: 200, message: 'Session Deleted' };
}).catch((err) => {
return err;
})
}
})