Accessing Firebase Firestore on AWS Lambda - firebase

I have following problem
I am writing a lambda function which is gets a post value offer an API, than checks in firebase firestore if the value is there and than replies to the client. Simple.
This is my code:
const serverless = require("serverless-http")
const express = require("express")
const app = express()
const bodyParser = require("body-parser")
const cors = require("cors")
const admin = require("firebase-admin")
var login = require("./test.json")
admin.initializeApp({ credential: admin.credential.cert(login) })
const db = admin.firestore()
app.use(cors())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.post("/", function(req, res) {
let result = req.body.code.toUpperCase()
db.collection("voucher")
.get()
.then(x => {
console.log("TEST")
console.log(x)
})
.catch(err => res.status(400).send({ err }))
})
module.exports.voucher = serverless(app)
The API works just fine, problem is connecting to the firestore, the error object I get always says:
{code: "MODULE_NOT_FOUND"}
I did it how it is shown in the tutorial here:
https://firebase.google.com/docs/firestore/quickstart
But it does not seem to work at all.
I downloaded the correct credentials, actually I gave myself admin access to everything. But still it does not work.
You guys have any suggestions?

Related

Why am I keep getting INTERNAL ERROR 500 with my firebase cloud function?

I was trying to deploy a cloud function on firebase but I keep getting this error. I deleted all of my logic to console the response and debug but nothing's changed.
I am sure that the problem is not related to permissions because the invocation is allowed for unauthenticated users.
this is the block of my function:
// Firebase config
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const cors = require("cors")({
origin: true
});
admin.initializeApp();
exports.emailMessage = functions.https.onCall((req, res) => {
return cors(req, res, async() => {
console.log(req);
console.log(res);
}).catch(() => {
res.status(500).send("error");
});
});

No permission error in Firebase Cloud Function

I want to implement a Slack authentication with Passport.js + Firebase Cloud Function. But when I redirected URL, the forbidden error occurs.
The error:
Your client does not have permission to get URL /api/auth/slack?uid=XXXXXXXXXXX&redirectTo=http://localhost:3000 from this server.
The React code:
const slackAuthorizeURL = (uid) =>
`https://us-central1-xxxxxxxxx.cloudfunctions.net/api/auth/slack?uid=${uid}&redirectTo=${window.location.href}`
<a href={slackAuthorizeURL}>Sign in with Slack</a>
The Server code:
const express = require('express')
const session = require('express-session')
const app = express()
const allowedOrigins = [
'http://localhost:3000',
]
const allowCrossDomain = (req, res, next) => {
const origin = req.headers.origin
if (allowedOrigins.includes(origin)) {
res.header('Access-Control-Allow-Origin', origin)
}
res.header('Access-Control-Allow-Methods', 'GET,POST')
res.header('Access-Control-Allow-Headers', 'Content-Type')
next()
}
app.use(allowCrossDomain)
app.use(session({ secret: config.session.secret }))
const passport = require('passport')
app.use(passport.initialize())
app.use(passport.session())
app.get('/auth/slack', (req, res, next) => {
req.session.uid = req.query.uid
req.session.redirectTo = req.query.redirectTo
passport.authenticate('slack')(req, res, next)
})
I have already set up allUsers to Cloud Functions Admin on api in Google Cloud Platform.
I missed to set up allUsers to Cloud Functions Admin on api in Google Cloud Platform correctly.
https://cloud.google.com/functions/docs/securing/managing-access#allowing_unauthenticated_function_invocation

Firestore Cloud Function Times Out When called

I have a custom endpoint setup for my FireStore database.
For now, all I want is to print all values to console, but when I call it from a client, the request times out and the console only says:
#firebase/database: FIREBASE WARNING: The Firebase database
'project-name' has been disabled by a database owner.
(https://project-name-de56eb8.firebaseio.com)
Here's my code. Can anyone tell me what is (what thins are) wrong with it?
const util = require('util');
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const language = require('#google-cloud/language');
const client = new language.LanguageServiceClient();
const express = require('express');
const app = express();
app.post('/calculateAverage', async (request, response) => {
const bodyUserId = request.body.id
let query = admin.database().ref(`/user_info/`);
try {
const snapshot = await query.once('value');
snapshot.forEach((childSnapshot) => {
console.log("key: " + childSnapshot.key + " value: " + childSnapshot.val())
});
response.send({"snapshot await": "ok"});
} catch(error) {
console.log('Error getting messages', error.message);
response.send({"snapshot await error": error.message});
}
});
exports.api = functions.https.onRequest(app);
The problem is that you no use firebase realtime data.
in the options of firebase you have database and next *Cloud Firestore and
*Realtime Database, select Realtime Database and after, active this option and with this the solution

TypeError: functions.database is not a function

I want to update or creat an object, but i have this error :"TypeError: functions.database is not a function" on the registry of firebase function
this is my code:
const functions = require('firebase-functions');
exports.actualizar = functions.https.onRequest((request, response) => {
const obj = request.body;
const MAC = obj.MAC;
functions.database().ref ('/sensores/{MAC}').update(obj).promise.then(() =>
{
console.log("UpDate Success");
return req.status(200).send("ok");
})
.catch(() => {
functions.database.ref('/sensores'). set(obj).promise.then(() =>{
console.log ("Created Succces");
return req.status(200).send("");
})
.catch(() =>{
console.log("Error");
return req.status(500).send("error");
})
})
});
You can't use the Cloud Functions for Firebase SDK to query the database. It's just used for building the function definition. To query your database or other Firebase products, you need to use the Firebase Admin SDK, or whatever SDK is normally used to do so.
For example, you will see lots of official sample code that starts like this:
const admin = require('firebase-admin'); // this is the Admin SDK, not firebase-functions
admin.initializeApp();
// Then use "admin" to reach into Realtime Database, Firestore, Cloud Storage, etc.

firebase cloud functions failed to access google sheet api

I want to build an api service that use google sheet api to create, read and write the spreadsheets. The api service is deployed on firebase cloud functions with express.js, I have created the service account, and use JWT for authentication. The api works (i.e. can create, read and write spreadsheets) when test locally, but fails when I deployed to firebase cloud functions.
const functions = require('firebase-functions');
// firebase admin
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
// google sheet api
const google = require('googleapis');
const key = require('./key/serviceKey.json');
const scopes = [
'https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/drive'
];
const authClient = new google.auth.JWT(
key.client_email, null, key.private_key, scopes, null
);
authClient.authorize((err, tokens) => {
if (err) {
console.log('error occurred when authorising with JWT');
} else {
console.log('auth success, tokens', tokens);
google.options({ auth: authClient });
}
});
const drive = google.drive('v2');
const sheets = google.sheets('v4');
The error message I get when I test it on firebase cloud functions:
Request is missing required authentication credential. Expected OAuth
2 access token, login cookie or other valid authentication credential.
Ultimately, I will have an ionic app that calls the api to perform certain actions (e.g. create and share spreadsheets with other user). So am I doing the right approach or I should use other types of authentication?
My function definitions:
const functions = require('firebase-functions');
const express = require('express');
const cors = require('cors')({ origin: true });
const bodyParser = require('body-parser');
const app = express();
app.use(cors);
app.use(bodyParser.json({ type: 'application/json' }));
// some routing setup
exports.api = functions.https.onRequest(app);
Router setup:
const express = require('express');
const router = express.Router();
router.post('/createSheets', (req, res) => {
const request = { // }
sheets.spreadsheets.create(request, (err, sheetsResponse) => {
// some code
}
});

Resources