From 90d7af71be36306569f0802e7dcd4e3af322fffb Mon Sep 17 00:00:00 2001 From: Callum Gilchrist <43007634+CallumGilly@users.noreply.github.com> Date: Sat, 1 Oct 2022 14:38:20 +0100 Subject: [PATCH] Added code which does something, not my dislike of firebase --- functions/index.js | 47 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/functions/index.js b/functions/index.js index 081873b..c4bdfe6 100644 --- a/functions/index.js +++ b/functions/index.js @@ -1,9 +1,46 @@ const functions = require("firebase-functions"); - +const admin = require("firebase-admin"); +admin.initializeApp(); +const db = admin.firestore(); // // Create and Deploy Your First Cloud Functions // // https://firebase.google.com/docs/functions/write-firebase-functions // -// exports.helloWorld = functions.https.onRequest((request, response) => { -// functions.logger.info("Hello logs!", {structuredData: true}); -// response.send("Hello from Firebase!"); -// }); +exports.messageTester = functions.https.onRequest((request, response) => { + functions.logger.info("Message was requested", {structuredData: true}); + response.send("Sending message"); +}); + + +exports.userCreated = functions.auth.user().onCreate((user) => { + return admin.auth().setCustomUserClaims(user.uid, { + admin: false, + role: undefined, + }); +}); + +exports.roleDefined = functions.https.onCall((data, context) => { + if (data.role == "firstAider") { + admin.auth().setCustomUserClaims(user.uid, { + role: "firstAider", + }); + } else { + return db.collection("users").doc(user.uid).set({ + role: "patient", + }); + } +}); + +exports.firstAiderCreated = functions.firestore.document("first-aiders/{userId}/") + .onCreate(async (snap, context) => { + admin.auth().setCustomUserClaims(snap.id, { + role: "firstAider", + }); + }); + +exports.firstAiderCreated = functions.firestore.document("patients/{userId}/") +.onCreate(async (snap, context) => { + admin.auth().setCustomUserClaims(snap.id, { + role: "patient", + }); +}); +//patient firstAider \ No newline at end of file