Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions functions/functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

const {onCall, HttpsError} = require('firebase-functions/v2/https')
const {initializeApp} = require("firebase-admin/app");
const {getDatabase} = require('firebase-admin/database');
const {getMessaging} = require('firebase-admin/messaging');

const sanitizer = require('./sanitizer');

Expand All @@ -25,12 +27,12 @@ initializeApp();
// [START allAdd]
// [START addFunctionTrigger]
// Adds two numbers to each other.
exports.addNumbers = onCall((data) => {
exports.addNumbers = onCall((request) => {
// [END addFunctionTrigger]
// [START readAddData]
// Numbers passed from the client.
const firstNumber = data.firstNumber;
const secondNumber = data.secondNumber;
const firstNumber = request.data?.firstNumber;
const secondNumber = request.data?.secondNumber;
// [END readAddData]

// [START addHttpsError]
Expand All @@ -56,11 +58,11 @@ exports.addNumbers = onCall((data) => {

// [START messageFunctionTrigger]
// Saves a message to the Firebase Realtime Database but sanitizes the text by removing swearwords.
exports.addMessage = onCall((data, context) => {
exports.addMessage = onCall((request) => {
// [START_EXCLUDE]
// [START readMessageData]
// Message text passed from the client.
const text = data.text;
const text = request.data?.text;
// [END readMessageData]
// [START messageHttpsErrors]
// Checking attribute.
Expand All @@ -70,7 +72,7 @@ exports.addMessage = onCall((data, context) => {
'one arguments "text" containing the message text to add.');
}
// Checking that the user is authenticated.
if (!context.auth) {
if (!request.auth) {
// Throwing an HttpsError so that the client gets the error details.
throw new HttpsError('failed-precondition', 'The function must be called ' +
'while authenticated.');
Expand All @@ -79,23 +81,23 @@ exports.addMessage = onCall((data, context) => {

// [START authIntegration]
// Authentication / user information is automatically added to the request.
const uid = context.auth.uid;
const name = context.auth.token.name || null;
const picture = context.auth.token.picture || null;
const email = context.auth.token.email || null;
const uid = request.auth.uid;
const name = request.auth.token.name || null;
const picture = request.auth.token.picture || null;
const email = request.auth.token.email || null;
// [END authIntegration]

// [START returnMessageAsync]
// Saving the new message to the Realtime Database.
const sanitizedMessage = sanitizeText(text); // Sanitize the message.
return admin.database().ref('/messages').push({
const sanitizedMessage = sanitizer.sanitizeText(text); // Sanitize the message.
return getDatabase().ref('/messages').push({
text: sanitizedMessage,
author: { uid, name, picture, email },
}).then(() => {
// Optionally send a push notification with the message.
if (data.push && context.instanceIdToken) {
return admin.messaging().send({
token: context.instanceIdToken,
if (request.data.push && request.instanceIdToken) {
return getMessaging().send({
token: request.instanceIdToken,
Comment thread
rootkiller6788 marked this conversation as resolved.
data: { text: sanitizedMessage },
});
}
Expand Down
Loading