diff options
author | Biswakalyan Bhuyan <biswa@surgot.in> | 2024-08-01 17:35:27 +0530 |
---|---|---|
committer | Biswakalyan Bhuyan <biswa@surgot.in> | 2024-08-01 17:35:27 +0530 |
commit | fb04271b5288e8fb5891b7d6326f4806d12b82d5 (patch) | |
tree | e459c3e1f8bb6e168becdddd0d48779135d91a7f /backend/functions/index.js | |
parent | 4bb13ee84f6bb51cba6544ccd0690ab2049512a9 (diff) | |
parent | b3c07fd9f1664dda4f16357aaca74dff8226401d (diff) | |
download | admin-panel-fb04271b5288e8fb5891b7d6326f4806d12b82d5.tar.gz admin-panel-fb04271b5288e8fb5891b7d6326f4806d12b82d5.tar.bz2 admin-panel-fb04271b5288e8fb5891b7d6326f4806d12b82d5.zip |
Merge remote-tracking branch 'project/master'
Diffstat (limited to 'backend/functions/index.js')
-rw-r--r-- | backend/functions/index.js | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/backend/functions/index.js b/backend/functions/index.js new file mode 100644 index 0000000..c6dabaa --- /dev/null +++ b/backend/functions/index.js @@ -0,0 +1,43 @@ +const functions = require('firebase-functions'); +const admin = require('firebase-admin'); +const cors = require('cors')({ origin: true }); + +admin.initializeApp(); + +// Function to add or update user roles +exports.addUserRole = functions.https.onCall(async (data, context) => { + // Verify that the request is made by an authenticated admin user + if (!context.auth || !context.auth.token.admin) { + throw new functions.https.HttpsError('permission-denied', 'Only admins can add user roles.'); + } + + const email = data.email; + const role = data.role; + + try { + const user = await admin.auth().getUserByEmail(email); + await admin.auth().setCustomUserClaims(user.uid, { role: role }); + return { message: `Success! ${email} has been made an ${role}` }; + } catch (error) { + return { error: error.message }; + } +}); + +// Function to get user roles +exports.getUserRoles = functions.https.onRequest(async (req, res) => { + cors(req, res, async () => { + const users = []; + try { + const listUsersResult = await admin.auth().listUsers(); + listUsersResult.users.forEach((userRecord) => { + users.push({ + email: userRecord.email, + role: userRecord.customClaims ? userRecord.customClaims.role : 'user' + }); + }); + res.status(200).send(users); + } catch (error) { + res.status(500).send({ error: error.message }); + } + }); +}); |